clearTimeout does not work
I'm trying to set a timeout for a stream using the change event listner. The timeout does start, however, the clearTimeout doesn't work. It does not stop the timer. I can see from the log where the section of the clear timeout (else clause) is working, but the timer does not stop.
sound=Ti.Media.createAudioPlayer({
url:audiobook.chapters[audiobook.nowplaying].url
});
sound.addEventListener('change', function(e){
Ti.API.info('state change ' + e.description);
var errorTimer=0;
if (e.description == "waiting_for_data") {
Ti.API.info('starting timeout');
errorTimer = setTimeout(function(){
Ti.API.info("could not find file");
sound.stop();
dlStatus.text = 'Error!';
playerNotes.text = 'File url cannot be accessed!';
}, 5000);
}
else {
Ti.API.info('clearng timeout');
clearTimeout(errorTimer);
}
//sound.stop();
});
Any ideas?
1 Answer
-
Accepted Answer
You should define your timer outside your event. And not inside. When you define it inside your event it will be created every time.