简体   繁体   English

从setTimeout()调用setInterval()不会启动计时器

[英]Calling setInterval() from setTimeout() does not start timer

I have a task to bring up a bootstrap modal window after a predefined time, for which I use setTimeout(). 我有一项任务是在预定义的时间后启动引导模式窗口,为此我使用setTimeout()。 Then another task is to show a countdown in the body of the modal window - however calling setInterval() from setTimeout() function does not do anything. 然后,另一个任务是在模​​态窗口的主体中显示倒计时-但是从setTimeout()函数调用setInterval()不会执行任何操作。

func = function(warntime) {
   func.tid = setInterval(function() {
     $("#dialog-countdown").html(warntime--);
       if(warntime == 0) {
           doSignout();
       }
   }, 1000);
};

var tmwarn = setTimeout(function(){
         $("#timeout-modal").modal("show");
         func(60);
   }, 20000);

So the func() call does not start the timer, at least not in Chrome or Safari or IE. 因此,func()调用不会启动计时器,至少在Chrome,Safari或IE中不会启动计时器。

What's wrong? 怎么了? Any workaround? 任何解决方法?

If I understand you correctly, you want something like that: 如果我对您的理解正确,那么您将需要以下内容:

func = function(warntime) {
   func.tid = setInterval(function() {
     warntime--;
     console.log('warntime: ', warntime);
       if(warntime == 0) {
           doSignout(func.tid);
       }
   }, 2000);
};

doSignout = function(id) {
   console.log('signing out');
   clearInterval(id) 
}

setTimeout(function(){
    func((sessionTimeout - sessionTimeoutWarning)/1000);
}, sessionTimeoutWarning);

here's the working fiddle 这是工作的小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM