I have the following code:
function initializeUI(){
//More code here
models.player.addEventListener('change:context', contextChanged);
//More code here
}
var timerId = null;
function contextChanged() {
models.player.load('context').done(function (player) {
if (player.context.uri == tempPlaylist.uri) {
//User started playing this context. Start timer.
timerId = setInterval(updateTimerView, 1000);
}
else {
//User is playing a different context. Delete temp playlist and timer.
models.Playlist.removeTemporary(tempPlaylist);
clearInterval(timerId);
}
});
}
function updateTimerView(){
//UI related code goes here
}
I would assume that change:context
would only be fired when the context (in my case, tempPlaylist
) is changed, but it turns out that it's also being fired when the track is changed, played, or paused. This can't be the desired functionality... otherwise it would defeat the purpose of having the change:track
and change:playing
events.
This is causing a lot of trouble for me mainly because when the track is changed, played, or paused, contextChanged
is called, but player.context.uri
hasn't changed, so more and more timers keep getting added. What am I missing here?