如何暂停和恢复jQuery间隔 [英] How to pause and resume jquery interval

查看:110
本文介绍了如何暂停和恢复jQuery间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用jQuery创建了一个自定义滑块.为此,我使用了setInterval函数:

timer = setInterval(function() {},  8000);

但是我无法暂停并恢复间隔.我有两个要使用的按钮(播放,暂停).可以说,我在3秒钟后单击暂停,然后重新开始.因此,它应该在该滑块中再停留5秒钟,然后转到下一个滑块,并继续每个8秒钟.我见过这种带有鼠标悬停的滑块,但我自己做不到.我已经尝试过了:

clearInterval(timer);

但是,这似乎重置了时间间隔,请不要暂停.谁能帮忙:)

解决方案

我不完全确定这是jQuery的固有特性,但是,您可以使用标志将其暂停,并检查是否要执行setInterval. /p>

找到了可能对您有用的东西, jquery-timer

或者,您可以跟踪由setInterval设置的ID,并在需要暂停时清除它们.然后,当您希望恢复时可以再次设置它:

var id = window.setInterval(<code>); //create
window.clearInterval(id); //pause
id = window.setInterval(<code>); //resume

I have made a custom slider with jQuery. For this I have used setInterval function:

timer = setInterval(function() {},  8000);

But I cannot pause and resume the interval. I have 2 buttons (play, pause) which I want to use for. Lets say I click pause after 3 sec, and then resume it. So it should stay in that slider for 5 more seconds and then go to the next one and continue 8 seconds each. I have seen this kinda slider with mouseover pause, but can't do it by myself. I have tried this:

clearInterval(timer);

But this seems reset the interval, don't pause. Can anyone help :)

解决方案

I'm not entirely sure that's something native to jQuery, however, you could use a flag to pause it, and check in your setInterval whether to execute.

Edit: Found something that might be useful to you, the jquery-timer

Alternitively, you can keep track of the id set by setInterval, and clear out out when you'd like to pause. Then you can set it again when you wish to resume:

var id = window.setInterval(<code>); //create
window.clearInterval(id); //pause
id = window.setInterval(<code>); //resume

这篇关于如何暂停和恢复jQuery间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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