Forum Discussion
Using Javascript to Gamify a Countdown Timer (incorrect answers subtract time)
Below is a JavaScript code. It assumes that another variable named "incorrect" is created within Storyline, initialized with a value of false. This variable switches to true when the timeline starts on the "Try Again" layer. In the code, it is switched back to false as soon as 10 seconds are subtracted from the timer. Maddie, this code may need constant updates based on your final scenario interaction and as the project scales. That's why I preferred Storyline alone over JavaScript, allowing those without JavaScript experience to easily update and progress. Attached is a short video demonstration of your revised project, along with the .story file for reference.
const player = GetPlayer();
let sec = player.GetVar("timer");
//Set up the timer
function startTimer(){
if (sec > 0) {
if (player.GetVar('incorrect')) {
if (sec < 10) { // Check if remaining time is less than 10 seconds
sec = 0; // Set timer to 0 if less than 10 seconds remaining
} else {
sec -= 10;
}
player.SetVar('incorrect', false); // Set incorrect to false after subtracting 10 seconds
} else {
sec -= 1;
}
player.SetVar("timer", sec);
if (sec <= 0) {
clearInterval(timerId);
}
}
}
//Start the timer
let timerId = setInterval(startTimer, 1000);
Please set the timer value to 60 in Storyline and remove the trigger that subtracts 10 seconds from the timer, as it is now managed with JavaScript.
- MaddieSchroe3077 months agoCommunity Member
Okay I understand! This was the solution I was looking for but I see what you mean about Storyline being easier for others to update, and that might be the way to go.