【发布时间】:2019-08-07 06:40:38
【问题描述】:
我正在尝试使用具有相同名称的嵌套timeOut,这实际上就像一个循环,而不是完全一样。
我尝试使用这个例子:
let i = 1;
setTimeout(function run() {
func(i);
setTimeout(run, 100);
}, 100);
来自这个link。
正如您在此链接中看到的,我不能使用interval 和loop。
这是我的实际代码:
let i = 0;
let x = setTimeout(async function run() {
if(i == 2) {
// I want to stop my x here completly
console.log(i)
clearTimeout(x);
}
try {
//some code here e.g:
console.log(10)
} catch (err) {
//some other code here e.g:
console.log(err)
}
i++;
x = setTimeout(run, 800);
}, 800);
我的输出:
10
10
2
10
10
... //never stops
我也看到了这个link,但不是我的情况。
任何人都可以做点什么让我完全停止x吗?
提前致谢。
【问题讨论】:
-
你需要在
clearTimeout之后从函数中return否则会在几行之后创建新的超时
标签: javascript settimeout cleartimeout