问题描述
我有一个按钮,可在div滚动时更改div的背景.背景需要在计时器上进行更改,因此我使用setTimout来执行更改背景的方法.我认为clearTimeout会取消并且设置了超时,因此我将其放在mouseleave事件上.但是,它似乎并没有停止超时.我的逻辑在这里吗?
I have a button that changes the background of a div when its rolled over. the background needs to change on a timer so i have used setTimout to execute methods that change the backgrounds. I thought clearTimeout would cancel and timeouts i have set so i put this on the mouseleave event. However it doesnt seem to stop the timeouts. Is my logic right here?
$("#h2Buzz").mouseenter(function () {
setTimeout(playV(), 2700);
setTimeout(playP(), 5400);
});
$("#h2Buzz").mouseleave(function () {
clearTimeout(playV());
clearTimeout(playP());
});
function playV() {
$("#ServicesBackgroundImage2").css("background-image", "url(/images/v.jpg)");
}
function playPn() {
$("#ServicesBackgroundImage2").css("background-image", "url(/images/p.jpg)");
}
推荐答案
为您的setTimeout
分配一个变量,然后将其传递给clearTimeout
进行清除,即
Assign a variable to your setTimeout
, which you then pass to clearTimeout
to clear it, i.e.
var play_timeout = setTimeout("playV()", 2700);
clearTimeout(play_timeout);
(请注意,我在第一个setTimeout
参数周围添加了引号)
(Note I added quotes around your first setTimeout
argument)
这篇关于对setTimeout和clearTimeout的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!