I created a function to understand the behavior of the setTimeout() function in javascript. I created a recursive function which keeps calling itself. It worked fine until I added a sleep function inside. Here's the code.
我创建了一个函数来理解javascript中setTimeout()函数的行为。我创建了一个不断调用自身的递归函数。它工作正常,直到我在里面添加了睡眠功能。这是代码。
'use strict';
(function iterate(i) {
var t=setTimeout(function () {
console.log('timeout ' + i + ' ' + (new Date()));
sleep(1000);
console.log('timeout ' + i + ' ' + (new Date()));
//clearTimeout(t);
iterate(++i);
},2000);
})(0);
function sleep(millis) {
console.log('sleeping...');
var date = new Date();
var curDate = null;
do {
curDate = new Date();
}
while (curDate - date < millis);
}
Here's the output when I run this code.
这是我运行此代码时的输出。
timeout 0 Wed May 13 2015 12:07:21 GMT+0530 (IST)
sleeping...
timeout 0 Wed May 13 2015 12:07:22 GMT+0530 (IST)
timeout 1 Wed May 13 2015 12:07:25 GMT+0530 (IST)
sleeping...
timeout 1 Wed May 13 2015 12:07:26 GMT+0530 (IST)
timeout 2 Wed May 13 2015 12:07:29 GMT+0530 (IST)
sleeping...
timeout 2 Wed May 13 2015 12:07:30 GMT+0530 (IST)
The difference between two iterate calls should be 2 seconds but it is always sleep_time + delay_time, which in this case is 3 seconds. Also adding clearTimeout() just before calling iterate(), makes it work normal.
两次迭代调用之间的差异应该是2秒,但它始终是sleep_time + delay_time,在这种情况下是3秒。在调用iterate()之前添加clearTimeout(),使其正常工作。
Can anyone explain what is happening here?
任何人都可以解释这里发生了什么?
2 个解决方案
#1
what is happening here is that you get your code stuck in a while loop for 1 second each time , before the setTimeout is recalled , that is why you get 3 seconds delay .
这里发生的事情是,在调用setTimeout之前,每次都会让你的代码在while循环中停留1秒钟,这就是你得到3秒延迟的原因。
#2
EDIT (I don't know why I assume that you sleep sleep(1000)
after both console.log
):
编辑(我不知道为什么我认为你在console.log后睡觉睡眠(1000)):
Are you sure it's correct output? When I run it, it generates expected values:
你确定它是正确的输出吗?当我运行它时,它会生成预期值:
timeout 0 Wed May 13 2015 09:16:16 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 0 Wed May 13 2015 09:16:17 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 1 Wed May 13 2015 09:16:19 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 1 Wed May 13 2015 09:16:20 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 2 Wed May 13 2015 09:16:22 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 2 Wed May 13 2015 09:16:23 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 3 Wed May 13 2015 09:16:25 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 3 Wed May 13 2015 09:16:26 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 4 Wed May 13 2015 09:16:28 GMT+0200 (Środkowoeuropejski czas letni)
So we have 1 second delay between console.log
and 2 seconds of iterate(i)
delay due to setTimeout
. It seems to be correct.
因此,我们在console.log和2秒迭代(i)由于setTimeout引起的延迟之间有1秒的延迟。这似乎是正确的。
#1
what is happening here is that you get your code stuck in a while loop for 1 second each time , before the setTimeout is recalled , that is why you get 3 seconds delay .
这里发生的事情是,在调用setTimeout之前,每次都会让你的代码在while循环中停留1秒钟,这就是你得到3秒延迟的原因。
#2
EDIT (I don't know why I assume that you sleep sleep(1000)
after both console.log
):
编辑(我不知道为什么我认为你在console.log后睡觉睡眠(1000)):
Are you sure it's correct output? When I run it, it generates expected values:
你确定它是正确的输出吗?当我运行它时,它会生成预期值:
timeout 0 Wed May 13 2015 09:16:16 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 0 Wed May 13 2015 09:16:17 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 1 Wed May 13 2015 09:16:19 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 1 Wed May 13 2015 09:16:20 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 2 Wed May 13 2015 09:16:22 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 2 Wed May 13 2015 09:16:23 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 3 Wed May 13 2015 09:16:25 GMT+0200 (Środkowoeuropejski czas letni)
VM631:15 sleeping...
VM631:8 timeout 3 Wed May 13 2015 09:16:26 GMT+0200 (Środkowoeuropejski czas letni)
VM631:6 timeout 4 Wed May 13 2015 09:16:28 GMT+0200 (Środkowoeuropejski czas letni)
So we have 1 second delay between console.log
and 2 seconds of iterate(i)
delay due to setTimeout
. It seems to be correct.
因此,我们在console.log和2秒迭代(i)由于setTimeout引起的延迟之间有1秒的延迟。这似乎是正确的。