如下代碼,用setInterval自然是沒問題,因為使用都是同一個timer,請問setTimeout怎么解決這個問題? function scrollbars(){ index++; if(index <= max){ target = index * singleHeight; }else{ index = 0; target = index * singleHeight; } $el.animate({'top': target}, 1000, function(){ timer = setTimeout(scrollbars, 2000); }); } timer = setTimeout(scrollbars, 2000); $('#container').on('mouseenter', function(){ // 這里鼠標移入,清除定時器,可能在清除之前,上面已經生成新的定時器了,所以只是清除了之前的定時器,這里可能無法起到停止的作用 clearTimeout(timer); });
關于清除定時器的問題,setTimeout
明月笑刀無情
2019-02-05 11:46:08