Forum Discussion

CajaPopularM704's avatar
CajaPopularM704
Community Member
3 months ago
Solved

Timer issue

Hello everyone!

Hoping you are having an amazing week, I am currently working on a project that requires a timer, this could be countdown or countup, but in this case I found (in the Articulate community) a countup timer.

I modified it so it could work with the variable Project.ElapsedTime, because I need it to run as much as the user is on the story. It responds to other variables I added, but for some reason, no variable I call to the JS is working, it stays on 0.

If anyone can help me with this, it would be great.

  • Try:

    var Total = getVar("Total_Time");
    
    var minutes = Math.floor(Total / 60);
    var seconds = Total % 60;
    
    var formattedTime = minutes + ' minute(s) ' + (seconds < 10 ? '0' : '') + seconds + ' second(s)';
    
    setVar("Split_Time", formattedTime);
    

    or:

    var Total = getVar("Total_Time");
    
    var minutes = Math.floor(Total / 60);
    var seconds = Math.floor(Total % 60);  // <-- Floor seconds to remove decimals
    
    var formattedTime = minutes + ' minute(s) ' + (seconds < 10 ? '0' : '') + seconds + ' second(s)';
    
    setVar("Split_Time", formattedTime);

    to remove decimals.

6 Replies

  • Nedim's avatar
    Nedim
    Community Member

    Try:

    var Total = getVar("Total_Time");
    
    var minutes = Math.floor(Total / 60);
    var seconds = Total % 60;
    
    var formattedTime = minutes + ' minute(s) ' + (seconds < 10 ? '0' : '') + seconds + ' second(s)';
    
    setVar("Split_Time", formattedTime);
    

    or:

    var Total = getVar("Total_Time");
    
    var minutes = Math.floor(Total / 60);
    var seconds = Math.floor(Total % 60);  // <-- Floor seconds to remove decimals
    
    var formattedTime = minutes + ' minute(s) ' + (seconds < 10 ? '0' : '') + seconds + ' second(s)';
    
    setVar("Split_Time", formattedTime);

    to remove decimals.

    • CajaPopularM704's avatar
      CajaPopularM704
      Community Member

      Hello Nedim

      Hoping You're ok.

      I've been having another issue, this time with a time limit, I want it to be 20 minutes, but for some reason no variable attached to the timer (var Split_Time) works, or even jumps immediately to the 20 minutes and stops the course.

      For example, I added a new variable, Time_End, and gave it a value of 0, and added a condition to jump to a slide if it becomes 20, because I attached it to the var minutes used in the code you provided me above. In this case, Time_End becomes 20 immediately and stops working.

      If you could help me, once more, I would be grateful. 

      • Nedim's avatar
        Nedim
        Community Member

        You can modify the existing script I shared earlier by adding a new True/False variable called TimeUp. Update the code to check when the total time reaches or exceeds 20 minutes (1200 seconds) and set TimeUp to true when that condition is met. Then, create a trigger to jump to the next slide when TimeUp changes to true. If you want a different time limit, simply update the condition in the script to the desired number of seconds, less or more than 1200 seconds, to fit your needs. 

        I’ve attached the updated Storyline file and the script on Slide 2.

        var Total = getVar("Total_Time");
        
        var minutes = Math.floor(Total / 60);
        var seconds = Math.floor(Total % 60);
        
        var formattedTime = minutes + " minute(s) " + (seconds < 10 ? "0" : "") + seconds + " second(s)";
        setVar("Split_Time", formattedTime);
        
        // Set TimeUp = true when 20 minutes (1200 seconds) is reached or exceeded
        if (Total >= 1200) {
            setVar("TimeUp", true);
        } else {
            setVar("TimeUp", false);
        }

        .