How to set a variable and use it in different functions | XM Community
Skip to main content

Hi, I want to set a variable. If people click one button within 10 seconds, we display some questions and ask people to click next button to proceed. If people don't click the button within 10 seconds, will automatically click the next button and go to next block. The question is I don't know how to define and use a global variable. In the following js, x_val is set to 1 within the block but after 10 seconds, the next function still works, it takes x_val as 0 and automatically proceeds to next block, which leads to even if people leave this block, it automatically jumps to the third block when people in the second block. How can i solve this, thanks!!

Qualtrics.SurveyEngine.addOnReady(function()
{
jQuery("#NextButton").hide();   
var x_val=0; // set this variable equal to 0.

jQuery('#Button').on('click', () => {
x_val=1; // set this variable equal to 1
jQuery("#NextButton").show();   
});

// after ten seconds, if button is not clicked, automatically click next button
setTimeout(function(){
if (x_val==0){
jQuery("#NextButton").click();
}
}, 10000);
}, 30000);
});

cm201623 - How about setting an embedded data at the end of JS function? The following syntax can be used:

Qualtrics.SurveyEngine.setEmbeddedData("globalVariable", localVariable);

Just remember to define 'globalVariable' as an embedded data in your survey flow.


Leave a Reply