简体   繁体   English

如何确保命令顺序按顺序运行(jQuery中的超时问题-Qualtrics)

[英]How to ensure sequence of commands run sequentially (Timeout issue in jQuery - Qualtrics)

I am designing an experiment in qualtrics using java script. 我正在使用Java脚本设计定性实验。 For each question in my study, a participant needs to guess the correct answer, type it in, and then hit the space button. 对于研究中的每个问题,参与者都需要猜测正确的答案,输入正确的答案,然后按空格键。 Once they hit the space button, a logic checks whether their answer is correct or not. 一旦他们按下空格键,逻辑就会检查他们的答案是否正确。 If the answer was correct, a sound should be played for 1.2 seconds, the background color should change into red for a second and then back to white, and lastly the experiment should automatically moves on to the next question. 如果答案正确,则应播放声音1.2秒钟,背景颜色应变为红色一秒钟,然后变为白色,最后,实验应自动转到下一个问题。

So far, I figured out the correct sequence of commands. 到目前为止,我已经弄清楚了正确的命令顺序。 However, it seems like i am not using the setTimeout logic correctly. 但是,似乎我没有正确使用setTimeout逻辑。 no matter what combination of timeout values I used, I couldn't get it play all the middles steps, including the audio play and red background flash, before the screen moves to the next question. 无论我使用哪种超时值组合,在屏幕移至下一个问题之前,都无法让它播放所有中间步骤,包括音频播放和红色背景闪烁。

Here, I 've shared my code. 在这里,我分享了我的代码。 I would very much appreciate any help. 非常感谢您的帮助。

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

    Qualtrics.SurveyEngine.addOnReady(function()
    {
        /*Place your JavaScript here to run when the page is fully displayed*/

        var qid = this.questionId;
        var changepage = null;

        /*focus on the box*/
        jQuery("#"+qid+" .InputText").select().focus();

        document.onkeydown = function(event) {
            console.log('keydown',event);
             if (event.which == 32) { //hit the space button
                event.preventDefault();
                 //read the input
                input = jQuery("#"+qid+" .InputText").val();
                console.log(input);

                 if (input.trim().toLowerCase() == "cat") {

                     //alert(input.trim().toLowerCase());
                    document.getElementById("correct").play(); //play the correct sound which is 1.2seconds long

                    setTimeout(jQuery("body").css("background-color","red"), 3000); // change the background color

                     setTimeout( jQuery('#NextButton').click(), 2000) //move on to the next question

                 } //end of if       
             } // end of if 32  
        }  //end of down button

    });

    Qualtrics.SurveyEngine.addOnUnload(function()
    {  
        jQuery("body").css("background-color","white");                 
    });

setTimeout is asynchronous. setTimeout是异步的。 If you want multiple setTimeouts to execute in a fixed order then the times have to be additive. 如果要以固定的顺序执行多个setTimeout,则必须增加时间。 So your NextButton timing should be 5000ms (3000ms + 2000ms). 因此,您的NextButton计时应为5000ms(3000ms + 2000ms)。

Alternatively, you could put the NextButton click inside the background color change setTimeout function. 或者,您可以将NextButton单击在背景颜色更改setTimeout函数中。 To add to that, could you put both inside an 'ended' event on your audio. 除此之外,您是否可以将两者都放在音频的“结束”事件中?

 if (input.trim().toLowerCase() == "cat") {

                 //alert(input.trim().toLowerCase());
                 //play the correct sound which is 1.2seconds long


                 vid.play();

                 vid.onended = function() 
                 { 
                     jQuery(".skirt").css("background-color","red");
                     jQuery('#NextButton').click();
                 };

             } //end of if

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM