可以在setInterval()中调用clearInterval()吗? [英] Can clearInterval() be called inside setInterval()?

查看:317
本文介绍了可以在setInterval()中调用clearInterval()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bigloop=setInterval(function () {
              var checked = $('#status_table tr [id^="monitor_"]:checked');
                if (checked.index()===-1 ||checked.length===0 || ){
                    bigloop=clearInterval(bigloop);
                    $('#monitor').button('enable');
                }else{

                        (function loop(i) {                           
                            //monitor element at index i
                            monitoring($(checked[i]).parents('tr'));
                            //delay of 3 seconds
                            setTimeout(function () {
                                //when incremented i is less than the number of rows, call loop for next index
                                if (++i < checked.length) loop(i);
                            }, 3000);
                        }(0)); //start with 0
                }                            
            }, index*3000); //loop period

我有上面的代码,有时它正在工作,有时它不是。我想知道如果clearInterval实际上清除了计时器?? ,因为有这个监视器按钮只有在<$ c时才会被禁用$ c>监控功能。当点击一个名为 .outputRemove 的元素时,我有另一个 clearInterval 。请参阅以下代码:

I have the code above and sometimes it is working, sometimes it is not. I am wondering if the clearInterval actually clear the timer?? because there is this monitor button that will only be disabled when it is in monitoring function. I have another clearInterval when an element called .outputRemove is clicked. See the code below:

//remove row entry in the table      
        $('#status_table').on('click', '.outputRemove', function () {
            deleted= true;
            bigloop= window.clearInterval(bigloop);
            var thistr=$(this).closest('tr');
            thistr.remove();
            $('#monitor').button('enable');

            $('#status_table tbody tr').find('td:first').text(function(index){
               return ++index;

            });
        });

但是在它再次被禁用之前它已经启用了一段时间。 clearInterval setInterval 函数中获取程序?

But it was enabled for a while before it is disabled again. Will clearInterval get the program out from the setInterval function?

推荐答案

是的,你可以。你甚至可以测试它:

Yes you can. You can even test it:

var i = 0;
var timer = setInterval(function() {
  console.log(++i);
  if (i === 5) clearInterval(timer);
  console.log('post-interval'); //this will still run after clearing
}, 200);

在此示例中,此计时器在<$ c $时清除c>我达到5。

In this example, this timer clears when i reaches 5.

这篇关于可以在setInterval()中调用clearInterval()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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