Forum Discussion

ToniLee-Ostrows's avatar
ToniLee-Ostrows
Community Member
4 years ago

Stop / reset JavaScript in a storyline

Hello,  I have a timer that I built from a Storyline provided in another post; it uses a javascript snippet to start the timer and works great however, I am trying to add a feature to stop the timer and/or reset it (both would be nice but either would be helpful).  Can anyone provide some help?  

  • Yeah the solution is fine. Only thing is that Sam already had that solution a few years back as he mailed me ;-)

    Only thing  i dislike is adding code time after time in the index file when publishing. Using a WebObject to inject it into the html would be better. Or edit the index file in Articulate Programmes folder then you only have to do it once.

  • You are right with the webobjects.

    I gave up using it for a while as there was a bug that I needed to rename the folder every time I changed a thing. 

    However! This is the great thing that Storyline offers many ways to solve the same problem. 

  • Thanks Christoph and Toni :)

    I think the original one I had was from Christoph actually, and he was kind enough to send here a version with separate script file. And thanks Toni for sending that here too. 

    It is also easily modified to countdown instead, so that's great.

  • After about 15 hrs of chatgpt helping me write code I've got something that works!

     

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

    var player = GetPlayer();
    var count = 20; // Set the countdown duration to 20 seconds
    var timerID = player.GetVar("Timer_ID"); // Retrieve the stored timer ID

    function startTimer() {
      timerID = setInterval(timer, 1000);
      player.SetVar("Timer_ID", timerID); // Store the timer ID in a Storyline variable
    }

    function stopTimer() {
      clearInterval(timerID);
    }

    function resetTimer() {
      stopTimer(); // Stop the previous timer
      count = 20; // Reset the countdown duration to 20 seconds
      var minutes = zeros(Math.floor(count / 60));
      var seconds = zeros(count % 60);
      var totalTime = minutes + ':' + seconds;
      player.SetVar("Countdown_Display", totalTime);
      player.SetVar("Timer_finished", 0);
      startTimer(); // Start the new timer
    }

    function timer() {
      if (player.GetVar("Reset_Timer") === 1) {
        resetTimer();
        player.SetVar("Reset_Timer", 0); // Reset the player variable
        return;
      }

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

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

      if (count <= 0) {
        player.SetVar("Timer_finished", 1);
        stopTimer(); // Stop the timer when countdown is finished
      }
    }

    // Clear the previous interval timer if the stored timer ID is valid
    if (timerID) {
      clearInterval(timerID);
    }

    // Initial setup
    resetTimer(); // Call the resetTimer() function initially to set up the countdown

    // Example usage: Set the player variable "Reset_Timer" to 1 to reset the timer
    player.SetVar("Reset_Timer", 1);

  • Nedim's avatar
    Nedim
    Community Member

    A script can be dynamically injected and appended to the head to work across the slides and scenes. In the attached demo, the timer can be paused, continued, or reset from any slide or scene without breaking the code. It consists of a single script in the master slide and a few triggers to ensure the Play/Pause button is properly toggled, based on whether the timer is running or not.

    • AndrsCaceres-68's avatar
      AndrsCaceres-68
      Community Member

      Thank you Nedim. Could you please share your storyline file, so I can see how did you manage to reset the timer when it's finished? I used the JS code provided by Jeremy, and indeed it resets the countdown before it's finished. But once the timer reaches 00:00 I can't reset the timer... Maybe I missed something (I started using JS in storyline a week ago...)