首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >可以在setInterval()内调用clearInterval()吗?

可以在setInterval()内调用clearInterval()吗?
EN

Stack Overflow用户
提问于 2013-05-17 09:04:51
回答 1查看 123.8K关注 0票数 174
代码语言:javascript
复制
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是否真的清除了定时器??,因为有这个monitor按钮,只有在monitoring功能中才会被禁用。当一个名为.outputRemove的元素被单击时,我有另一个clearInterval。请参见以下代码:

代码语言:javascript
复制
//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 function中取出程序吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-17 09:10:18

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

代码语言:javascript
复制
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);

在此示例中,当i达到5时,此计时器将清除。

票数 299
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16599878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档