简体   繁体   English

setTimeout不在for循环内触发

[英]setTimeout is not firing inside for loop

I have the following jquery code with setTimeout function to set a variable to false but it never works and the variable always remains to be true 我有以下带有setTimeout函数的jquery代码,将变量设置为false,但它永远不起作用,并且该变量始终保持为true

      for( var ff = 0; ; ff++)
      {
        if( dif == 0){
          break;
        }
        if (locked){
          // locked = false;
          setTimeout( function(){ locked = false; },2000);
        }
        else{
          LeftKeyPressed();
          locked = true ;
          setTimeout( function(){ locked = false; },3000);
          dif--;
        }
      }

can anyone to help in how to set the locked variable to false after exactly two seconds from setting it to true. 在将锁定变量设置为true的两秒钟后,任何人都可以帮助您如何将锁定变量设置为false。

Here's a fiddle of this issue . 是这个问题的一个小提琴

Okey, since the requirements are what they are I dont know what exactly you want but this is closest I could manage to make: http://jsfiddle.net/db6gJ/ Okey,因为需求就是它们,所以我不知道您到底想要什么,但是我可以设法做到这一点: http : //jsfiddle.net/db6gJ/

It calls method three times and gives you a change to do what you want to do when locked is false or true. 它会调用方法三遍,并在锁定为false或true时更改您要执行的操作。 Plus, it won't block the event loop (with that infinite for loop) so your site can make other tasks while timeOut call's are running. 此外,它不会阻塞事件循环(带有无限for循环),因此您的站点可以在timeOut调用运行时执行其他任务。

javascript code also here: javascript代码也位于此处:

var locked = true,
    timesMax = 3,
    timeCurrent = 0;

var inputLoop = function() {

    var delay = 2000;

    if(locked) {

        // Do something when locked is true
        console.log("locked is true!");

        locked = false;

    } else {

        // Do something when locked is false
        console.log("locked is false!");

        locked = true;
        delay = 3000;

    }

    timeCurrent++;

    // call again after delay, but call only 3 times
    if(timeCurrent < timesMax) {
        setTimeout(inputLoop, delay);
    }

};

// Launch it
inputLoop();

If you view your javascript console (in chrome: right mouse click -> inspect element -> console) it should print: 如果您查看JavaScript控制台(在Chrome浏览器中:右键单击->检查元素->控制台),则应打印:

locked is true!
locked is false! 
locked is true! 

Also to be noted: your original code had alert('out'); 还要注意:您的原始代码具有alert('out'); when it's done, but to be honest, code will continue execution elsewhere while your "inputLoop" code is not doing anything else than waiting callback to be run and it's the way it should be. 完成后,但老实说,代码将在其他地方继续执行,而您的“ inputLoop”代码除了等待回调运行之外没有做任何其他事情,这就是应该这样做的方式。

If you wan't to know when it is called last time you could modify one of the previous lines to be: 如果您不想知道上次调用的时间,则可以将前几行修改为:

// call again after delay, but call only 3 times
if(timeCurrent < timesMax) {
    setTimeout(inputLoop, delay);
} else { 
    // Last call in this method
    console.log('done!');
}

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

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