How to clear var value on submit? And why is my code functional only under "Preview", and not live? | XM Community
Skip to main content

Hi! I have two issues.
1) The built-in Qualtrics Timer sucks and resets the countdown when page is refreshed. Wrote own solution, see code below. Here, if page is refreshed, it'll still move forward after 18 secs.
RefreshValue is undefined or 0 when page is first loaded, so it skips the first "if' conditional. RefreshValue set to 1 after initial load. First "if" conditional is triggered if page is refreshed to make sure page is forced to move forward after initial 18 seconds runs out.
When a MC option is chosen, I need the page to go to next page and the variable "RefreshValue" to be reset. My survey only has 1 Q per page.
Issue: If a MC option is picked (this.questionclick), page does move forward (this.clickNextButton), but RefreshValue is not reset to 0. This causes 18sec countdown to carry over from last page instead of restarting.
What's not working in the "this.questionclick" function? Could the "Qualtrics.SurveyEngine.addOnPageSubmit" function work to reset the variable? Just not sure where/how I would add it to this.
2) For some reason, my custom code seems to be functional when I test under "Preview" or "View Block", but does not work at all when I test it via a live link on my browser. Why is that happening?
Any and all help would be appreciated. Thank you. Here is a copy of my code.
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

// if page is refreshed
if(RefreshValue == 1) {

//current time when page is refreshed
var TimeEnd = new Date();

// Time passed between last load and refresh
var TimePassed = TimeEnd - TimeStart;

// New start time is the current refresh's load time
// in case page is refreshed more than once
var TimeStart = TimeEnd;

// Maximum time allowed on page updated (in milliseconds)
var TimeRemaining = TimeRemaining - TimePassed;

// Maximum time allowed (in seconds)
var delay = TimeRemaining/1000;

//if option is selected, head to next page and reset RefreshValue
this.questionclick = function(event,element){
if (element.type == 'radio') {
RefreshValue = 0;
this.clickNextButton();
}
}

//if option is not selected, go to next page automatically after
// max time (delay) allowed expires and reset Refresh Value
var that = this;
(function(){
RefreshValue = 0;
that.clickNextButton();
}).delay(delay);
}

// initial page load (when RefreshValue is not 1)
else {

//RefreshValue set to 1 to indicate
//page has been loaded
var RefreshValue = 1;

//log in current time when page is first loaded
var TimeStart = new Date();

//18 seconds in milliseconds, maximum time allowed on page
var TimeRemaining = 18000;

//18 seconds in seconds, maximum time allowed on page
var delay = TimeRemaining/1000;

//if option is selected, head to next page and reset RefreshValue
this.questionclick = function(event,element){
if (element.type == 'radio') {
RefreshValue = 0;
this.clickNextButton();
}
}

//if option is not selected, automatically go to next page after
//max time (delay, 18 secs) expires, and reset RefreshValue
var that = this;
(function(){
that.clickNextButton();
RefreshValue = 0;
}).delay(delay);

}


});

Be the first to reply!

Leave a Reply