[英]Why does this simple script never end?
I have a simple JavaScript function, that looks like this: 我有一个简单的JavaScript函数,如下所示:
countDown(); function countDown() { var count = 10; document.write(count); if (count > 0) { count = count-1;; setTimeout(countDown, 1000); } }
Why does the variable count
never change? 为什么变量count
永远不变? This function never ends . 此功能永无止境。 . 。 . 。
Because the countDown() function sets the variable count to ten every time it is called. 因为countDown()函数每次将变量count设置为十。 A slight scoping change will make the function behave as you might have intended. 轻微的作用域更改将使该功能按预期运行。
var count = 10;
countDown();
function countDown() {
document.write(count);
if (count > 0) {
count = count-1;;
setTimeout(countDown, 1000);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.