Forum Discussion
Restart Course, but keep value of one variable
Hi Jürgen,
May I ask you one more thing:
I have got this countdown JS:var player = GetPlayer();
var zeit = player.GetVar("zeit");
var downloadTimer = setInterval(function(){
if(zeit < 2){
clearInterval(downloadTimer);
}
zeit -= 1;
var player = GetPlayer();
player.SetVar("zeit", zeit);
}, 1000);
It works fine, but now I am looking for a way to reset the counter.
As it is now, the counter counts down to 0.
But if the user has successfully completed the task within the given time, I need to reset the counter for the next task.
I tried changing the value of "zeit", as this is the variable that JS loads.
But unfortunately that does not work. I attached a sample file.
here a small patch
function clearTimer(){
if( window.downloadTimer > 0 ){
clearInterval( window.downloadTimer );
window.downloadTimer = -1;
console.log( "stopped" );
}
}
clearTimer();
window.downloadTimer = setInterval( function(){
var zeit = GetPlayer().GetVar("zeit");
console.log( "zeit", zeit );
if(zeit < 2){
clearTimer();
}
if(zeit > 0){
zeit -= 1;
}
GetPlayer().SetVar( "zeit", zeit );
}, 1000);
important: save the timer number in the window object, so other scripts can interrupt the timer
https://360.articulate.com/review/content/c8452454-0f9d-4144-bde2-d681821a76aa/review
hint: use for formating scripts here in the forum the function "Formats > Blocks > Pre" not "Formats > Inline > Code"