Forum Discussion

LoriDresner's avatar
LoriDresner
Community Member
2 years ago

JavaScript Countdown Timer

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);
  • SandeepGadam's avatar
    SandeepGadam
    Community Member

    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.

  • KevGriffiths's avatar
    KevGriffiths
    Community Member

    Hello there, apologies for jumping in here so late but this was exactly what I have been looking for! 

    I changed it to a 30 minute countdown which works great.

    The only thing is the countdown ends and stays on 00:01 is there a way to make it end on 00:00?

    Many thanks in advance.

  • SandeepGadam's avatar
    SandeepGadam
    Community Member

    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.

  • KevGriffiths's avatar
    KevGriffiths
    Community Member

    Hi Sandeep, thank you so much for getting back to me on this. 
    Unfortunately, that didn't work, it still just stops at 00:01

  • SandeepGadam's avatar
    SandeepGadam
    Community Member

    My bad, let me take a closer look and see what I can do better with the code and make sure it stops at 00:00. Please accept my sincere apologies.

  • KevGriffiths's avatar
    KevGriffiths
    Community Member

    No worries at all, your help is very much appreciated. I'm fairly new to Java so this is all great learning for me :) 

    • SandeepGadam's avatar
      SandeepGadam
      Community Member

      Hello Kev, would you be willing to share your project file here so that I can take a closer look and see if I can adjust the timer to stop at 00:00? I just want to give it a try at my end and try to provide you with an alternative code.

      Thank you!!

  • SandeepGadam's avatar
    SandeepGadam
    Community Member

    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!!