Forum Discussion
JavaScript Count Up Timer
I needed to add a timer to a software simulation project, but all I could find was a count down timer. I'm a still a JavaScript beginner, so I asked Google and was not disappointed!
Here's the code:
// Create Storyline variables for seconds and minutes
var player = GetPlayer();
player.SetVar("seconds", 0);
player.SetVar("minutes", 0);
// JavaScript function to count seconds and reset every minute
function countSeconds() {
var seconds = player.GetVar("seconds");
var minutes = player.GetVar("minutes");
seconds++;
player.SetVar("seconds", seconds);
if (seconds >= 60) {
seconds = 0;
player.SetVar("seconds", seconds);
minutes++;
player.SetVar("minutes", minutes);
}
}
// Start the timer using setInterval
setInterval(countSeconds, 1000); // 1000 milliseconds = 1 second