Javascript Timer
Apr 26, 2023
Hi all. I have a java script timer that executes on a slide. When I replay the screen, I need it to reset the timer. What happens right now, is that it seems to add a new timer along with the previous timer, and they interfere with each other, given times from each timer back and forth. Is there a way that I can clear the previous timer out when I replay this screen? Here is the javascript that I am executing on the slide:
var player=GetPlayer();
function countdown( elementName, minutes, seconds )
{
var endTime, hours, mins, msLeft, msTotal, time;
function twoDigits( n )
{
return (n <= 9 ? "0" + n : n);
}
function updateTimer()
{
msLeft = endTime - (+new Date);
if ( msLeft < 1000 ) {
player.SetVar("timer", "0:00");
} else {
time = new Date( msLeft );
hours = time.getUTCHours();
mins = time.getUTCMinutes();
player.SetVar("timer", (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() ));
setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
}
}
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
msTotal = endTime - (+new Date);
updateTimer();
}
countdown( "timer", 4, 00 );
1 Reply
Share a storyline file with this... much easier to debug.
My best guess without having a Storyline sample to play with...
use a variable for your timeout.
var initialTimeOut = setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
Then whenever you go to a next slide or for whatever reason you need to clear the timer you can call it like this.
clearTimeout( initialTimeOut );
Ofcourse then you have to set the initialTimeOut again to use the timer.