function test() {
for (var i = 0; i < 500000; i++) {
var p = document.createElement('p');
p.setAttribute('id', 'testp');
document.body.appendChild(p);
document.body.removeChild(p);
}
var date = new Date();
document.write("test:" + date.getMinutes() + ':' + date.getSeconds() + ':' + date.getMilliseconds() + '<br>');
}
var num = 0;
var i = setTimeout(function() {
num++;
if (num > 10) {
clearTimeout(i);
}
test();
var date = new Date();
document.write("间隔:" + date.getMinutes() + ':' + date.getSeconds() + ':' + date.getMilliseconds() + '<br>');
i = setTimeout(arguments.callee, 1000);
}, 1000);
代码类似上面这种,停上不了循环执行操作
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
clearTimeout之后再加个return才行.另外test()里循环太多了...卡了页面
改成
不需要
clearTimeout不太清楚你的意思你是需要在num>10的时候停止timer么?是的话看代码。不是请看这个意思
你只要判断符合条件后不进行下一次的settimeout调用就停止了
你的settimeout已经执行完毕了所以不必要清理了不需要调用cleartimeout方法了
加个
else就好了。