clearInterval();不工作 [英] clearInterval(); not working

查看:78
本文介绍了clearInterval();不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个图像滚动器,该滚动器每隔几秒钟(为了调试而需要10秒)或当用户单击图像时自动使图像前进.我的代码(如下所示)有效,但是如果完成了图像的手动(单击)推进,我想重置" 10秒计数器.我怎样才能做到这一点?我似乎对clearInterval没有运气.

I'm making an image scroller that automatically advances the image every few seconds (10 seconds for the sake of debugging) or when the user clicks the image. My code (shown below) works, although I'd like to "reset" the 10 second counter if a manual (click) advancement of the image is done. How can I do this? I seem to be having no luck with clearInterval.

... 
setInterval('gallery()',10000);
$("#gallery").click(function() {
clearInverval();// <--- not working
gallery();
});
…

我看到其他人将变量定义为(以我为例)setInterval('gallery()',10000);,但是我也无法使它起作用=/

I see others defining a variable as (in my case) setInterval('gallery()',10000); but I couldn't get that to work either =/

PS我对C以外的语言了解有限

PS I have limited knowledge of languages other than C

推荐答案

setInterval方法返回间隔的句柄,您可以使用该句柄停止该间隔:

The setInterval method returns a handle to the interval, which you use to stop it:

var handle = window.setInterval(gallery,10000);
$("#gallery").click(function() {
  window.clearInterval(handle);
  gallery();
});

这篇关于clearInterval();不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆