简体   繁体   English

setTimeout似乎正在立即执行

[英]setTimeout seems to be executing instantly

I am trying to teach myself some JavaScript and play around with recursion at the same time. 我正在尝试教自己一些JavaScript,并同时尝试递归。 The code I've written below I expect to print "inhale" to the console 10 times with a 5-second delay in between printing. 我在下面编写的代码预计将“吸气”到控制台打印10次,两次打印之间有5秒的延迟。 However when I watch the console in Chrome's dev tools, all entries get printed seemingly instantaneously after refreshing the page. 但是,当我在Chrome的开发工具中查看控制台时,刷新页面后,所有条目似乎都立即被打印出来。 Can anyone help me find the error in my implementation? 谁能帮助我找到实施中的错误? Thank you! 谢谢!

function breathe(type, counter, limit, duration) {
  if(counter <= limit) {
    setTimeout(console.log(type), duration);
    counter++;

    return breathe(type, counter, limit, duration);
  }

  console.log("Finished!");
}

var breathing = breathe("inhale", 1, 10, 5000);

The setTimeout method expects a function ref. setTimeout方法需要一个函数引用。 This should work. 这应该工作。

function breathe(type, counter, limit, duration) {
  if(counter <= limit) {
    setTimeout(function(){console.log(type)}, duration);
    counter++;

    return breathe(type, counter, limit, duration);
  }

  console.log("Finished!");
}

var breathing = breathe("inhale", 1, 10, 5000);

This is because you are executing your console statement immediately (since you are executing console.log via the () ), rather than passing in a function reference to setTimeout : 这是因为您正在立即执行console语句(因为您正在通过()执行console.log ),而不是将函数引用传递给setTimeout

setTimeout(console.log(type), duration);

Try this instead: 尝试以下方法:

setTimeout(function() {
    console.log(type)
}, duration);  

Another form you could use (just to hammer-home the concept of passing a function reference) is this: 您可以使用的另一种形式(只是为了敲击传递函数引用的概念)是:

function callMeAfterDelay() {
    console.log(type);
}

setTimeout(callMeAfterDelay, duration);
setTimeout(console.log(type), duration);

This is immediately executing console.log(type) , and then passing the result into setTimeout. 这将立即执行console.log(type) ,然后将结果传递到setTimeout中。 The result of console.log is undefined , so nothing happens when the timeout goes off. console.log的结果是undefined ,因此超时结束后不会发生任何事情。

Instead, you want to do this: 相反,您想这样做:

setTimeout(function () {
    console.log(type);
}, duration);

I think this is what you want: 我认为这是您想要的:

function breathe(type, counter, limit, duration) {
  if(counter <= limit) {
    setTimeout(function(){
        console.log(type);
        counter++;

        return breathe(type, counter, limit, duration);
    }, duration);
  }

  console.log("Finished!");
}

var breathing = breathe("inhale", 1, 10, 5000);

 var counter = 0; var duration = 2000 var text = "Inhale"; var limit = 10; function print(counter, duration, text, limit) { setTimeout(function(){ console.log(text, counter) counter++; if (counter < limit) { print(counter, duration, text, limit); } },duration) }; print(counter, duration, text, limit); 

Try this. 尝试这个。

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

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