HTML and CSS Reference
In-Depth Information
this.play = function(){
track.play();
};
this.stop = function(){
track.pause();
};
}
As you can see, there is a method called
this.togglePlay
. The toggle play
method will check the track's state. If it is stopped or paused, it will trigger the
play
method; if it is playing, it will trigger the
stop
method. These conditions are
wrapped within a
switch
statement. The
switch
statement is a good alternative
to using
if
statements to reduce clutter. The statement is formed of the
following:
switch(value){
case condition:
/** condition code **/
break;
case condition:
/** condition code **/
break;
default:
/** default code **/
break;
}
As you can see, it takes a
value
. Each
case
represents a
condition
to compare
the
value
to. If the
condition
matches, it executes the code within the
case
and
then breaks out of the
switch
. If none of the
condition
s match, you can specify
a default action to take using
default
:. It's best practice to only compare integer
values in a
switch
statement.
With the
togglePlay
method complete, the
this.play
and
this.stop
methods
both act as wrappers to pause or play the track.
The full code for the track is as follows:
app.track = function(length){
this.state = {
STOPPED: 0,
PLAYING: 1,
PAUSED: 2
};