在Javascript/jQuery中重新启动setInterval()(无clearInterval) [英] Restarting a setInterval() in Javascript/jQuery (without clearInterval)

查看:86
本文介绍了在Javascript/jQuery中重新启动setInterval()(无clearInterval)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究使用jQuery构建的ui选项卡.一切正常,除了一个问题-我做了一个setInterval,它运行一个执行触发器("click")的函数,以便在5000毫秒后转到下一个选项卡.它在每个选项卡上运行都很好,问题在于,如果用户手动单击选项卡,则setInterval的计时器不会从0重新开始.例如,如果用户以0毫秒的时间在tab1上启动并在以下时间单击tab2 2000毫秒,setInterval不会回到0,它将从2000开始并运行到5000毫秒,随后将进入tab3.我了解为什么会发生这种情况,我只是想知道是否有一种方法可以重启setInterval计时,而不必执行clearInterval()并创建一个全新的setInterval().任何见识将不胜感激.

I'm working on ui tabs built using jQuery. Everything works except for one issue - I did a setInterval that runs a function that does a trigger("click") so that it goes to the next tab after 5000 miliseconds. It runs through each tab fine, the issue is that if the user manually clicks on a tab, the timer for the setInterval does not restart back at 0. For example if a user were to start on tab1 at 0 miliseconds and clicks on tab2 at 2000 miliseconds, the setInterval doesn't go back to 0, it would start at 2000 and run to 5000 miliseconds and would subsequently goto tab3. I understand why it's happening, I just wonder if there were a way to restart the setInterval timing without having to do a clearInterval() and creating an entirely new setInterval(). Any insight would be appreciated.

更新

感谢您的答复-我试图避免使用clearInterval的原因是因为我遇到了如何以clearInterval完全停止setInterval的方式编写代码的问题.该代码被设置为在用户单击选项卡时进行跟踪.问题是自动切换功能利用触发(点击"),所以它运行调用clearInterval功能我也写在标签自动变化.它似乎可以很好地运行,但是一旦用户开始单击选项卡,setInterval的行为就会异常,并且会意外地切换选项卡.我怀疑正在发生的事情是同时运行多个setIntervals ...这是代码(如果您还没有猜到的话,我在javascript/jquery上还很新).我已经注释掉了各个部分,以便它可以正常工作,但是仍然无法按我的预期工作(从第一篇文章开始).

Thanks for the replies - The reason I was trying to avoid using clearInterval was because I was having issues of how to write the code in a way where the clearInterval would stop the setInterval completely. The code is setup to track whenever a user has clicked a tab. The problem is the auto change function utilizes trigger('click'), so it runs the clearInterval function I wrote also when the tabs auto-change. It seems to run fairly fine on its own, but once the user starts clicking on tabs, the setInterval behaves unusually and switches tabs unpredictably. I suspect what is happening is that several setIntervals are running at once... Here's the code (If you haven't guessed it already, I'm pretty new at javascript/jquery). I've commented out parts so that it's functional, but it still doesn't function as I intended (from first post).

// auto change tabs
            if( options.interval ) {

                function timerCom() {
                    if( !$(".controller").hasClass('paused') ) {
                        var i = $(".tab-current > a").attr("rel");
                        //alert(i);
                        if( i == 3 ) {i = 0};
                        $container
                            .find('a')
                            .eq(i)
                            .trigger('click');
                    }    
                }

                //$("#promo-items > li > a").click(function () {
                    //var timer;
                    //if( timer != null ) {clearInterval(timer);}
                    timer = setInterval(timerCom, options.interval);

                //});

            }

推荐答案

不,没有清除计时器就无法重启由setInterval设置的计时器的方法.

No, there is no way to restart a timer set by setInterval without clearing the timer.

这篇关于在Javascript/jQuery中重新启动setInterval()(无clearInterval)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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