Forum Discussion
Restart Course, but keep value of one variable
I have a course with a lot of variables in use.
Resetting all used variables manually is error prone, so I am using the "restart course"-trigger that sets back everything.
It is kind of a quiz with a restricted amount of tries.
Therefor, I have a number-variable, with a default-value of 3, and substract 1 from that variable for each try.
When the variable is 0, no more tries are left and the quiz ends.
The problem is: restarting the course also sets back the variable that counts the number of tries.
I do can change the variable after the restart-trigger and set it to a certain number. But as it is already set back to its default value (3), substracting does not work anymore (as it will always end up being 2).
Does anyone know a way to keep the value of my variable, while setting back all other variables? Or some other work-around?
- Jürgen_Schoene_Community Member
you can save/restore variables in the global window object with some lines of javascript
example: var1 is permanent, var 2 not
trigger on button click restart (before execute "restart course")
var value = GetPlayer().GetVar("var1");
window.savedValue = value;trigger on start timeline ot the first slide
var value = window.savedValue;
if( value !== undefined ){
GetPlayer().SetVar("var1", value );
window.savedValue = undefined;
}result:
https://360.articulate.com/review/content/2c375a55-d99a-4f05-b6e0-8e9eaa9ff2d1/review
- StefanKoler-a6fCommunity Member
Hi Jürgen,
Thank you.
That works well.
Unfortunately, if I reload the page also variables stored in the global window are restored to there initial value. Can I store them somewhere else?
The project is part of an exam, so users should really only have a defined number of attempts.- Jürgen_Schoene_Community Member
here is an example where the counter is permanently saved in localStorage
trigger on button click restart
var value = GetPlayer().GetVar("var1");
localStorage.setItem("countTest12345", value);trigger on start timeline ot the first slide
var value = localStorage.getItem("countTest12345");
if( value !== null ){
GetPlayer().SetVar("var1", parseInt(value) );
}https://360.articulate.com/review/content/3c4845c0-6b8b-46d8-b25f-a3f581b53752/review
this remains until you delete the Local Storage manually
CAUTION: the solution does not protect against manipulation by the learner - you can simply overwrite the value in the browser
- StefanKoler-a6fCommunity Member
Perfect, thank you so much Jürgen.
- StefanKoler-a6fCommunity Member
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.- Jürgen_Schoene_Community Member
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"