检查Javascript中是否存在活动超时

检查Javascript中是否存在活动超时,javascript,jquery,settimeout,Javascript,Jquery,Settimeout,有没有办法确定是否有活动计时器 我有不同持续时间的n定时器,例如: Timer 1 -> 2-sec Timer 2 -> 8-sec .. ... Timer n -> n-sec 我需要知道所有计时器什么时候结束 HTML <div id="time-out-1"> Time out 1:<span></span> </div> <div id="time-out-2"> Time out

有没有办法确定是否有活动计时器

我有不同持续时间的n定时器,例如:

Timer 1 -> 2-sec

Timer 2 -> 8-sec

..

...

Timer n -> n-sec
我需要知道所有计时器什么时候结束

HTML

<div id="time-out-1">
   Time out 1:<span></span>
</div>

<div id="time-out-2">
   Time out 2:<span></span>
</div>

<button>
   Are all timers finished ?
</button>


注意:我需要这个特定示例的解决方案,因为在我的项目中,有n个js文件可能有计时器,它们的声明类似于这个示例,这可能会对您有所帮助

//if n Timer then take count n
var count = 2;

setTimeout(function () {
        count--;
        $("#time-out-1 span").text("Finished !");
 },2000);


  setTimeout(function () {
        count--;
        $("#time-out-2 span").text("Finished !");
 },8000);

 $('button').click(function(){
        //Check if all Timers are finished
        if(count==0)
        //finished
 });

您始终可以添加控制变量

var timer1_active = true,
    timer2_active = true;
setTimeout(function () {
    timer1_active = false;
    $("#time-out-1 span").text("Finished !");
},2000);


setTimeout(function () {
    timer2_active = false;
    $("#time-out-2 span").text("Finished !");
},8000);

$('button').click(function(){
    //Check if all Timers are finished
    var finished = !timer1_active && !timer2_active;
});

我会用jQuery提供的
承诺来实现这一点。考虑一下这个问题:

首先,我们为
承诺对象创建一个数组

var timers = [];
然后我们创建
promise对象本身:

var timer1promise = $.Deferred();
var timer2promise = $.Deferred();
var timer3promise = $.Deferred();
将它们推送到阵列:

timers.push(timer1promise);
timers.push(timer2promise);
timers.push(timer3promise);
像普通计时器一样创建计时器,但让每个计时器解析相应的promise对象:

var timer1 = setTimeout(function() { console.log('timer 1 finished'); timer1promise.resolve(); }, 1000);
var timer2 = setTimeout(function() { console.log('timer 2 finished'); timer2promise.resolve(); }, 2000);
var timer3 = setTimeout(function() { console.log('timer 3 finished'); timer3promise.resolve(); }, 3000);
创建一个在promise数组中的每个promise都已解析时“监视”的对象:

$.when.apply($, timers).then(function()
{
    console.log('All timers done!');
});

更多信息:

以下是我的做法,围绕本机函数创建一个包装器

(function(w) {
     var active = {};

     var _setTimeout = w.setTimeout;
     var _clearTimeout = w.clearTimeout;

     w.setTimeout = function(fn, delay) {
         var id = _setTimeout(function() {
             fn();
             delete active[id];
         }, delay);
         active[id] = true;
         return id;
     }

     w.clearTimeout = function(id) {
         delete active[id];
         _clearTimeout(id);
     }

     w.activeTimers = function() {
         return Object.keys(active).length > 0;
     }
})(window);
然后像这样使用它

setTimeout(function () {
    $("#time-out-1 span").text("Finished !");
},2000);


setTimeout(function () {
    $("#time-out-2 span").text("Finished !");
},8000);

$('button').click(function(){
    if ( window.activeTimers() ) {
        // still something going on
    } else {
        // all done !
    }
});

我认为无法通过对计时器的引用来确定计时器是否处于活动状态,因为引用只是一个没有属性的整数。如果您能提供您希望实现的目标的详细信息,我们可能会建议一个替代方案。如果您的超时没有交错/延迟,您是否只需要检查是否发生了最长的超时?在这种情况下,您只能在此基础上切换一个布尔值。@Quantastical可能会产生意外的结果。如果这些计时器是由第三方代码设置的,则您必须重写
setTimeout()/cleartTimeout()
方法来跟踪任何正在运行的计时器,而您实际上不想做任何事情。。。这看起来真的像是XY问题,但我可能错了等等,为什么它被否决了???看起来是有效的解决方法,可能不是最好的,但有效
setTimeout(function () {
    $("#time-out-1 span").text("Finished !");
},2000);


setTimeout(function () {
    $("#time-out-2 span").text("Finished !");
},8000);

$('button').click(function(){
    if ( window.activeTimers() ) {
        // still something going on
    } else {
        // all done !
    }
});