[英]Why does window.setTimeout() become to a loop call after using window.setInterval()
I want to count from 5 to 1 so I write code in Chrome console like this : 我想从5数到1,所以我在Chrome控制台中这样编写代码:
var a = 5;
window.setInterval (function() {
if(a > 0)
console.log(a--);
}, 1000);
window.clearInterval();
The console shows this result: 控制台显示以下结果:
5
4
3
2
1
And then I run another piece of code after it: 然后在它之后运行另一段代码:
var a = 5;
window.setTimeout (function() {
if(a > 0)
console.log(a--);
}, 1000);
And I find the console shows this result: 我发现控制台显示了以下结果:
5
4
3
2
1
Why setTimeout()
becomes a loop ? 为什么
setTimeout()
成为循环? It was supposed to print a single 5 !!! 它应该只打印一个5 !
window.clearInterval()
doesn't do anything by itself, and redeclaring a variable with var
will just assign to the original variable, so when you set var a = 5;
window.clearInterval()
本身不会执行任何操作,并且使用var
重新声明变量只会将其分配给原始变量,因此当您将var a = 5;
设置var a = 5;
the original setInterval
callback continues to run. 原始的
setInterval
回调将继续运行。
Pass clearInterval
the return value of setInterval
instead: 传递
clearInterval
代替setInterval
的返回值:
var a = 5;
var timer = setInterval(function() {
if (a > 0)
console.log(a--);
}, 1000);
// after output has been produced
clearInterval(timer);
If you want clearInterval
to happen automatically once a
hits 0
, put it inside the callback: 如果你想
clearInterval
一次自动发生a
命中0
,把它回调里面:
var a = 5;
var timer = setInterval(function() {
if (a > 0)
console.log(a--);
else
clearInterval(timer);
}, 1000);
You are wrongly anticipating that setTimeout has become a loop. 您错误地预料到setTimeout已成为循环。
This is what happens. 这就是发生的情况。 First you run the following code
首先,您运行以下代码
var a = 5;
window.setInterval (function() {
if(a > 0)
console.log(a--);
}, 1000);
window.clearInterval();
In above code the callback function will be called every second and if value of a is greater than 0 it will decrement it by 1. Note that window.clearInterval()
line of code has no effect here so the callback function will keep on running but just stop printing to console because condition will not fall true when value of a is less than 0 or equal to 0 在上面的代码中,回调函数将每秒调用一次,如果a的值大于0,则会将其递减1。请注意,
window.clearInterval()
代码行在此处无效,因此回调函数将继续运行,但只是停止打印到控制台,因为当a的值小于0或等于0时条件将不成立
Now you run the following code 现在,您运行以下代码
var a = 5;
window.setTimeout (function() {
if(a > 0)
console.log(a--);
}, 1000);
This will first set the value of a
to 5 这将第一值设置
a
至5
Since you again set the value of a to 5 the setInterval
callback is still being executed every second. 由于您再次将a的值设置为5,因此
setInterval
回调仍在每秒执行一次。 So now the condition will evaluate to true
and hence it prints to console. 因此,现在条件将评估为
true
,因此将其打印到控制台。
setTimeout
is actually being printed by setInteval
setTimeout
打印到控制台的内容以及您想到的内容实际上是由setInteval
打印的 If you run the following code you will see what is acutally happeing 如果您运行以下代码,您将看到异常的快乐
var a = 5;
var timer = setInterval (function() {
if(a > 0)
console.log('From setInterval : ',a--);
console.log('SetInterval is still running');
}, 1000);
var a = 5;
window.setTimeout (function() {
if(a > 0)
console.log(a--);
}, 1000);
Hope this clears your doubt conceptually. 希望这可以从概念上消除您的疑问。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.