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

Qualtrics.SurveyEngine.addOnReady(function()
{
  /*Place your JavaScript here to run when the page is fully displayed*/
  
jQuery("#NextButton").hide();   

var x_val=0; // set this variable equal to 0. I know this doesnt work in later local functions. How to define it?

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

// after ten seconds, hide timer, show MissServiceMessage
setTimeout(function(){
if (x_val==0){
jQuery("#NextButton").click();
}
}, 10000);
}, 30000);
});

Try defining variable like this.
var x_val=0; //define it at the top

Qualtrics.SurveyEngine.addOnReady(function()
{
  /*Place your JavaScript here to run when the page is fully displayed*/
  
jQuery("#NextButton").hide();

jQuery('#Button').on('click', () => {
x_val=1; // don't use 'var x_val' as variable is already defined at the top.
});

// after ten seconds, hide timer, show MissServiceMessage
setTimeout(function(){
if (x_val==0){
jQuery("#NextButton").click();
}
}, 10000);
}, 30000);
});


https://www.qualtrics.com/community/discussion/comment/37761#Comment_37761Thanks!! 😁


https://www.qualtrics.com/community/discussion/comment/37761#Comment_37761Hi, I just find it doesn't work well. It sets x_val=1 in the first on click function. But in the second function (after 30 seconds), it still takes x_val=0 and clicks the next button automatically after 10s. this error leads to when I am already in the next block, this code in the previous block still works and jumps to the next block.


I have now noticed that the code is missing second setTimeout function, let me know what exactly you are trying to achieve from this code.


https://www.qualtrics.com/community/discussion/comment/37920#Comment_37920If people click the #Button within 10 seconds, I wanna display some messages and show the next button so they can click the next button by themselves. If they don't click the #Button within 10 seconds (setTimeout function), the next button is automatically clicked and people proceed to next block.
Current js cannot set the variable value, in the setTimeout function, it always has x_val=0, which leads to even if people click the #Button within 10 seconds and proceed to next block, this setTimeout function in this block still runs and forces people to go to the third block.

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();   
jQuery("#NextButton").hide();   });
// after ten seconds, automatically click next button
setTimeout(function(){
if (x_val==0){
jQuery("#NextButton").click();
}
}, 10000);
}, 30000);
});


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


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


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


https://www.qualtrics.com/community/discussion/comment/37922#Comment_37922sorry it doesnt work


Can you send me your survey preview link?


https://www.qualtrics.com/community/discussion/comment/37924#Comment_37924e.g., if you click the quit in the first block, proceed to next block and do nothing, the block automatically clicks next button and ends survey (that is because the settimeout function in the first block)


Try this code and make sure quit button id is same as defined in code (i.e. #Button)
Qualtrics.SurveyEngine.addOnReady(function () {
    var x_val = 0; // set this variable equal to 0.
    jQuery("#NextButton").hide();


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


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


https://www.qualtrics.com/community/discussion/comment/37926#Comment_37926Hi, thank you mentioning the clearTimeout function, but it still does not work 😓


That survey link isn't working anymore, can you share it again? Also did you verify the button ID as well?


https://www.qualtrics.com/community/discussion/comment/37928#Comment_37928Thank you for your help. Please find the attached preview. I verified the button ID and I paste the entire js here.
https://nbs.az1.qualtrics.com/jfe/form/SV_afshDFBknR4Y3Lo
if we click the quit in the first block, automatically enters the second block. But in the second block, it will automatically proceed to next block (end survey) because of the first block settimeout function.

Qualtrics.SurveyEngine.addOnload(function() {
});

Qualtrics.SurveyEngine.addOnReady(function()
{
  /*Place your JavaScript here to run when the page is fully displayed*/
   
jQuery("#Msg1").show();   // show first message
jQuery("#QuitQButton").show(); // show "quit the queue" button

  jQuery("#Msg3").hide();
jQuery("#QuitMsg").hide();
jQuery("#SvcMsg").hide();
jQuery("#MissMsg").hide();

jQuery("#SvcButton").hide();
jQuery("#NextButton").hide();

var WT = 30000; // this round waiting time

var quit = 0;
var ontime = 0;

// if dont quit and dont click finish service button on time
var timeout = setTimeout(function(){
if (quit ==0 && ontime==0){
jQuery("#SvcButton").hide();
jQuery("#MissMsg").show();
jQuery("#NextButton").click();
}
}, WT+10000);

// Clicks the quit button: set embedded data equal to 1, show the fourth message, next button

jQuery('#QuitQButton').on('click', () => {
var r = confirm("Reminder: If you quit the system now, you will not get any bonus. You will proceed to the survey."); 
if (r == true) {
Qualtrics.SurveyEngine.setEmbeddedData("Quit1", 1);
quit = 1;
jQuery("#QuitQButton").hide();
jQuery("#QuitMsg").show();
jQuery("#NextButton").click();
if (timeout) {
      clearTimeout(timeout);
}
}
});

// Hide waiting time buttion, show third message, finish service check button
setTimeout(function(){
jQuery("#QuitQButton").hide();
jQuery("#Msg3").show();     // show the third message
jQuery("#SvcButton").show(); // show "finish my service" button

jQuery('#SvcButton').on('click', () => {
Qualtrics.SurveyEngine.setEmbeddedData("Svc1", 1);
jQuery("#SvcButton").hide();
jQuery("#SvcMsg").show();
jQuery("#NextButton").show(); // need to click the next button to proceed to survey
jQuery("#MissMsg").hide();
ontime = 1;
if (timeout) {
      clearTimeout(timeout);
}
});
}, WT);

});

Qualtrics.SurveyEngine.addOnUnload(function()
{
  /*Place your JavaScript here to run when the page is unloaded*/


});


Your code is looking fine but maybe your logic is not written correctly, I tried to understand what you are trying to achieve and based on that this is the code, I'm not sure if it's what you are looking for.
Qualtrics.SurveyEngine.addOnload(function () {


});






Qualtrics.SurveyEngine.addOnReady(function ()


{


    /*Place your JavaScript here to run when the page is fully displayed*/






    jQuery("#Msg1").show();   // show first message


    jQuery("#QuitQButton").show(); // show "quit the queue" button


    jQuery("#Msg3").hide();


    jQuery("#QuitMsg").hide();


    jQuery("#SvcMsg").hide();


    jQuery("#MissMsg").hide();


    jQuery("#SvcButton").hide();


    jQuery("#NextButton").hide();


    var WT = 30000; // this round waiting time


    var quit = 0;


    var ontime = 0;


// if dont quit and dont click finish service button on time


    var timeout = setTimeout(function () {


        if (quit == 0 && ontime == 0) {


            jQuery("#SvcButton").hide();


            jQuery("#MissMsg").show();


            jQuery("#NextButton").click();


        }


    }, WT + 10000);


// Clicks the quit button: set embedded data equal to 1, show the fourth message, next button


    jQuery('#QuitQButton').on('click', () => {


        var r = confirm("Reminder: If you quit the system now, you will not get any bonus. You will proceed to the survey.");


        if (r == true) {


            Qualtrics.SurveyEngine.setEmbeddedData("Quit1", 1);


            quit = 1;


            jQuery("#QuitQButton").hide();


            jQuery("#QuitMsg").show();


//            jQuery("#NextButton").click();


            if (timeout) {
                clearTimeout(timeout);
            }
            if (timeout2) {
                clearTimeout(timeout2);
            }


        }


    });


// Hide waiting time buttion, show third message, finish service check button


    var timeout2 = setTimeout(function () {


        jQuery("#QuitQButton").hide();


        jQuery("#Msg3").show();     // show the third message


        jQuery("#SvcButton").show(); // show "finish my service" button






        jQuery('#SvcButton').on('click', () => {


            Qualtrics.SurveyEngine.setEmbeddedData("Svc1", 1);


            jQuery("#SvcButton").hide();


            jQuery("#SvcMsg").show();


            jQuery("#NextButton").show(); // need to click the next button to proceed to survey


            jQuery("#MissMsg").hide();


            ontime = 1;


            if (timeout) {


                clearTimeout(timeout);


            }


        });


    }, WT);
});

Qualtrics.SurveyEngine.addOnUnload(function ()
{
    /*Place your JavaScript here to run when the page is unloaded*/
});



https://www.qualtrics.com/community/discussion/comment/37931#Comment_37931my logic is if quit (quit=1), automatically clicks next button and goes to next block. if not quit, after some time, if click the service button on time (ontime=1), show the next button, people can click by themselves. if not quit and not click the service button on time (quit = 0 & ontime = 0), automatically proceeds to next block.
i appreciate your help but may i ask why you are using

if (timeout) {
                clearTimeout(timeout);
            }

i use alert and find timeout value is 18. what does this mean? and in your answer, variable timeout2 is defined after the
if (timeout) {               
clearTimeout(timeout);
            }
            if (timeout2) {
                clearTimeout(timeout2);
            }
so the code doesnt work at all


clearTimeout prevents the code execution inside timeout function.
18 is just id assigned to the timeout variable so that it can be used in clearTimeout.
Apart from that, I think timeout function is working fine, some other code is not written correctly.


Leave a Reply