不是一個答案,但完整的代碼表現出相同的行爲:
(function() {
var counter = 0;
function testCounter() {
if (counter === 2) {
clearInterval (timer);
timer = setInterval (myWork, 1000);
}
else if (counter === 5)
clearInterval (timer);
}
function myWork() {
document.body.innerHTML += ++counter + ' ' + Date() +'<br/>';
}
document.body.innerHTML += 'Starting ' + Date() +'<br/>';
var timer = setInterval (myWork, 10000);
setInterval (testCounter, 500);
})();
此代碼顯示:
Starting Thu Sep 30 2010 16:15:09 GMT+0200 (CEST)
1 Thu Sep 30 2010 16:15:19 GMT+0200 (CEST)
2 Thu Sep 30 2010 16:15:29 GMT+0200 (CEST)
,並沒有更多的,如果我刪除的setInterval上testCounter並在myWork中調用它我得到以下內容:
Starting Thu Sep 30 2010 16:20:50 GMT+0200 (CEST)
1 Thu Sep 30 2010 16:21:00 GMT+0200 (CEST)
2 Thu Sep 30 2010 16:21:10 GMT+0200 (CEST)
3 Thu Sep 30 2010 16:21:11 GMT+0200 (CEST)
4 Thu Sep 30 2010 16:21:12 GMT+0200 (CEST)
5 Thu Sep 30 2010 16:21:13 GMT+0200 (CEST)
非常好奇.....
來源
2010-09-30 14:23:40
HBP
myWork是一個全局函數嗎? – methodin 2010-09-30 13:22:32