Javascript - 可以检查间隔是否已经设置? [英] Javascript - Possible to check if an interval is already set?

查看:55
本文介绍了Javascript - 可以检查间隔是否已经设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用间隔每 5 秒弹跳一次的 div.

I have a div that is bouncing every 5 seconds using an interval.

滚动到页面底部时,这个div淡出,间隔清零.

When scrolling to the bottom of the page, this div fades out and the interval is cleared.

但是,我认为间隔被多次创建并且自身重叠存在问题.

However, I think there is an issue with the interval being created multiple times and overlaps upon itself.

有没有办法检查是否设置了间隔,如果设置了,清除它,如果没有,设置它?

Is there a way to check if an interval is set, and if so clear it, and if not, to set it?

之所以需要清除interval,是因为jquery的弹跳效果导致div即使隐藏了也会再次出现.

The reason I need to clear the interval is because the bounce effect of jquery causes the div to appear again even if it's hidden.

JSBIN:http://jsbin.com/ijuhok/4/

推荐答案

您需要覆盖现有的间隔,以便您可以从任何地方清除它:http://jsbin.com/ijuhok/5/.

You need to overwrite the existing interval so that you can clear it from everywhere: http://jsbin.com/ijuhok/5/.

$j("#more").fadeIn('slow',function(){
    ResInterval = window.setInterval(bounceMore, 5000);
    // no "var"
});

您可以消除 window$(document).ready 因为它始终可用.

You can eliminate $(document).ready for window because it's always available.

这篇关于Javascript - 可以检查间隔是否已经设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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