JavaScript Countdown Timer

Mar 06, 2023

Hi all. I need to add a countdown timer to one of my Storyline slides so people don't skip through it too quickly. I already have a JavaScript code I've borrowed from someone for a different project, but it seems that the page I got it from no longer exists, so I cannot figure out how to change it. It is currently set to 35 seconds; how would I change it to 5 minutes? Thanks in advance!

function zeros(i) {

if (i < 10) {

 i = "0" + i;

}

return i;

}

var counter = setInterval(timer, 1000);

var fin="Done";

var player = GetPlayer();

var count = player.GetVar("Countdown_Duration");

function timer() {

 count = count - 1;

 minutes = zeros(Math.floor(count / 60));

 seconds = zeros(count % 60);

 if (count == 0){

 player.SetVar("Countdown_Finished",fin);

}

 if (count < 0) {

 clearInterval(counter);

 return;

 }

 totalTime = minutes + ':' + seconds;

 player.SetVar("Countdown_Display",totalTime);

}

 minutes = zeros(Math.floor(count / 60));

 seconds = zeros(count % 60);




if (count == 0){

 player.SetVar("Countdown_Finished",fin);

}

if (count < 0) {

 clearInterval(counter);

 return;

}

totalTime = minutes + ':' + seconds;

player.SetVar("Countdown_Display",totalTime);
11 Replies
Sandeep Gadam

Hi there, I made a few changes to your existing code. I set the countdown duration to 5 minutes (300 seconds) by assigning count the value of 300.

function zeros(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}

var counter = setInterval(timer, 1000);
var fin = "Done";
var player = GetPlayer();
var count = player.GetVar("Countdown_Duration");

function timer() {
count = count - 1;
minutes = zeros(Math.floor(count / 60));
seconds = zeros(count % 60);

if (count <= 0) {
player.SetVar("Countdown_Finished", fin);
clearInterval(counter);
return;
}

totalTime = minutes + ':' + seconds;
player.SetVar("Countdown_Display", totalTime);
}

count = 300; // Set the countdown duration to 5 minutes (300 seconds)

minutes = zeros(Math.floor(count / 60));
seconds = zeros(count % 60);

totalTime = minutes + ':' + seconds;
player.SetVar("Countdown_Display", totalTime);

Try this and let me know if this works.

Sandeep Gadam

Hello Kev, in the above code you modify the "timer()" function as follows:

function zeros(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}

var counter = setInterval(timer, 1000);
var fin = "Done";
var player = GetPlayer();
var count = player.GetVar("Countdown_Duration");

function timer() {
count = count - 1;

if (count < 0) {
count = 0; // Set the countdown to 0 when it goes below 0
}
minutes = zeros(Math.floor(count / 60));
seconds = zeros(count % 60);

if (count <= 0) {
player.SetVar("Countdown_Finished", fin);
clearInterval(counter);
return;
}

totalTime = minutes + ':' + seconds;
player.SetVar("Countdown_Display", totalTime);
}

count = 300; // Set the countdown duration to 5 minutes (300 seconds)

minutes = zeros(Math.floor(count / 60));
seconds = zeros(count % 60);

totalTime = minutes + ':' + seconds;
player.SetVar("Countdown_Display", totalTime);

Try this and let me know if this works.

Sandeep Gadam

Kev would you mind trying the below code once again before sharing your SL file? If this doesn't work too, I request you to please share your SL file.

function zeros(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}

var counter = setInterval(timer, 1000);
var fin = "Done";
var player = GetPlayer();
var count = player.GetVar("Countdown_Duration");

function timer() {
count = count - 1;
minutes = zeros(Math.floor(count / 60));
seconds = zeros(count % 60);

if (count <= 0) {
player.SetVar("Countdown_Finished", fin);
clearInterval(counter);
return;
}

totalTime = minutes + ':' + seconds;
player.SetVar("Countdown_Display", totalTime);

if (minutes == "00" && seconds == "00") {
clearInterval(counter);
}
}

count = 300; // Set the countdown duration to 5 minutes (300 seconds)

minutes = zeros(Math.floor(count / 60));
seconds = zeros(count % 60);

totalTime = minutes + ':' + seconds;
player.SetVar("Countdown_Display", totalTime);

 

Thank you!!