Forum Discussion
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?
- ChristophKri821Community Member
Sorry but I can not get the point. What is bad with the date object?
The original request was a timer which can be paused and reset. Never said mine is different to others. Sam just asked me in an email if I could provide the file I have created few years ago.
- MathNotermans-9Community Member
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. - ChristophKri821Community Member
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.
- SamG-84fad5d4-bCommunity Member
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.
- JeremyTrott-098Community Member
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);- AndrsCaceres-68Community Member
It works thank you so much!
- MathNotermans-9Community Member
15 hours of chatGPT? oh my in that time you can learn the basics of Javascript...