clearinterval 似乎没有停止旋转
我的主页上有一个用 jquery 制作的旋转横幅。它使用以下间隔,
var countBlackButtons = $(".blackitems").size();
var changeTrigger = 0;
var changeIndex = 1;
function startNewChange() {
clearInterval(changeIndex);
changeTrigger = setInterval(function () {
changeIndex = changeIndex + 1;
if(changeIndex != (countBlackButtons + 1)) {
$("#hpbutton" + changeIndex).trigger("click");
} else {
changeIndex = 0;
}
//$("#homepageCaroselHolder").css({ "background": "url( " + eval("itembg" + changeIndex) + ")" });
}, 4000);
这与我想在有人单击按钮时停止旋转完全不同。我研究了clearinterval,认为这会做我需要它做的事情:
$("#hpbutton1").click(function() {
currentItem = itembg1;
itemcount = 1;
$("#homepageCaroselHolder").fadeOut(function() {
$("#homepageCaroselHolder").css({ "background": "url(/media/284/jqueryhompepagecaroselbackground.jpg)" });
$("#homepageCaroselHolder").fadeIn();
});
//clear interval
clearInterval(changeTrigger);
});
但它只是继续旋转......还有什么我可以用来停止旋转吗?
I have a rotating banner on my homepage made in jquery. it uses the following interval
var countBlackButtons = $(".blackitems").size();
var changeTrigger = 0;
var changeIndex = 1;
function startNewChange() {
clearInterval(changeIndex);
changeTrigger = setInterval(function () {
changeIndex = changeIndex + 1;
if(changeIndex != (countBlackButtons + 1)) {
$("#hpbutton" + changeIndex).trigger("click");
} else {
changeIndex = 0;
}
//$("#homepageCaroselHolder").css({ "background": "url( " + eval("itembg" + changeIndex) + ")" });
}, 4000);
this is running perfectly apart from i want to stop the rotation when someone clicks a button. i looked into clearinterval and thought this would do what i need it to do:
$("#hpbutton1").click(function() {
currentItem = itembg1;
itemcount = 1;
$("#homepageCaroselHolder").fadeOut(function() {
$("#homepageCaroselHolder").css({ "background": "url(/media/284/jqueryhompepagecaroselbackground.jpg)" });
$("#homepageCaroselHolder").fadeIn();
});
//clear interval
clearInterval(changeTrigger);
});
but the it just carries on rotating... is there anything else i could use to stop the rotation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是因为如果您多次调用
startNewChange
,您将运行多个间隔,因为clearInterval(changeIndex);
不会清除任何内容(应该是清除间隔(changeTrigger);
)It's probably due to the fact that if you call
startNewChange
multiple times you will have multiple intervals running sinceclearInterval(changeIndex);
doesn't clear anything (should beclearInterval(changeTrigger);
)