有没有一种方法可以消除所有的休息时间?

时间:2021-11-12 01:15:19

Is there a way to clear all time outs from a given window? I suppose the timeouts are stored somewhere in the window object but couldn't confirm that.

是否有一种方法可以清除给定窗口中的所有超时?我认为超时存储在窗口对象的某个地方,但无法确认。

Any cross browser solution is welcome.

任何跨浏览器的解决方案都是受欢迎的。

6 个解决方案

#1


103  

They are not in the window object, but they have ids, which (afaik) are consecutive integers.

它们不在窗口对象中,但它们有id,而id (afaik)是连续整数。

So you may clear all timeouts like so:

所以你可以像这样清除所有的超时:

var id = window.setTimeout(function() {}, 0);

while (id--) {
    window.clearTimeout(id); // will do nothing if no timeout with id is present
}

#2


61  

I think the easiest way to accomplish this would be to store all the setTimeout identifiers in one array, where you can easily iterate to clearTimeout() on all of them.

我认为实现这一点的最简单方法是将所有的setTimeout标识符存储在一个数组中,在这个数组中,您可以轻松地迭代到clearTimeout()。

var timeouts = [];
timeouts.push(setTimeout(function(){alert(1);}, 200));
timeouts.push(setTimeout(function(){alert(2);}, 300));
timeouts.push(setTimeout(function(){alert(3);}, 400));

for (var i=0; i<timeouts.length; i++) {
  clearTimeout(timeouts[i]);
}

#3


10  

I have an addition to Pumbaa80's answer that might be useful for someone developing for old IEs.

我在Pumbaa80的答案中增加了一项内容,可能对为老年人开发的人有用。

Yes, all major browsers implement timeout ids as consecutive integers (which is not required by specification). Althrough the starting number differs form browser to browser. It seems that Opera, Safari, Chrome and IE > 8 starts timeout ids from 1, Gecko-based browsers from 2 and IE <= 8 from some random number that is magically saved across tab refresh. You can discover it yourself.

是的,所有主要浏览器都将超时id实现为连续整数(这不是规范所要求的)。不同浏览器之间的起始数量不同。Opera、Safari、Chrome和IE > 8从1启动超时id,从2启动基于geck的浏览器,从某个随机数字IE <= 8启动超时id,然后通过tab刷新神奇地保存下来。你可以自己发现。

All that meens that in IE <=8 the while (lastTimeoutId--) cycle may run over 8digits times and show the "A script on this page is causing Internet Explorer to run slowly" message. So if you can not save all you timeout id's or don't want to override window.setTimeout you may consider saving the first timeout id on a page and clearing timeouts until it.

所有这一切意味着在IE <=8的while (lastTimeoutId——)周期可能超过8位数,并显示“这个页面上的一个脚本导致Internet Explorer运行缓慢”消息。如果不能保存所有超时id或不想重写窗口。setTimeout您可以考虑保存页面上的第一个超时id并清除超时,直到它发生。

Execute the code on early page load:

在页面加载的早期执行代码:

var clearAllTimeouts = (function () {
    var noop = function () {},
        firstId = window.setTimeout(noop, 0);
    return function () {
        var lastId = window.setTimeout(noop, 0);
        console.log('Removing', lastId - firstId, 'timeout handlers');
        while (firstId != lastId)
            window.clearTimeout(++firstId);
    };
});

And then clear all pending timeouts that probably was set by foreign code so many times you want

然后清除所有可能是由外国代码设置的超时

#4


5  

How about store the timeout ids in a global array, and define a method to delegate the function call to the window's.

在全局数组中存储超时id,并定义一个方法来将函数调用委托给窗口。

GLOBAL={
    timeouts : [],//global timeout id arrays
    setTimeout : function(code,number){
        this.timeouts.push(setTimeout(code,number));
    },
    clearAllTimeout :function(){
        for (var i=0; i<this.timeouts.length; i++) {
            window.clearTimeout(this.timeouts[i]); // clear all the timeouts
        }
        this.timeouts= [];//empty the id array
    }
};

#5


1  

Use a global timeout which all of your other functions derive timing from. This will make everything run faster, and be easier to manage, although it will add some abstraction to your code.

使用全局超时值,所有其他函数都从该超时值派生计时。这将使一切运行得更快,也更容易管理,尽管它将为代码添加一些抽象。

#6


1  

We've just published a package solving this exact issue.

我们刚刚发布了一个解决这个问题的软件包。

npm install time-events-manager

npm安装time-events-manager

With that, you can view all timeouts and intervals via timeoutCollection & intervalCollection objects. There's also a removeAll function which clears all timeouts/intervals both from the collection and the browser.

这样,您可以通过timeoutCollection & intervalCollection对象查看所有超时和间隔。还有一个removeAll函数,它清除集合和浏览器的所有超时/间隔。

#1


103  

They are not in the window object, but they have ids, which (afaik) are consecutive integers.

它们不在窗口对象中,但它们有id,而id (afaik)是连续整数。

So you may clear all timeouts like so:

所以你可以像这样清除所有的超时:

var id = window.setTimeout(function() {}, 0);

while (id--) {
    window.clearTimeout(id); // will do nothing if no timeout with id is present
}

#2


61  

I think the easiest way to accomplish this would be to store all the setTimeout identifiers in one array, where you can easily iterate to clearTimeout() on all of them.

我认为实现这一点的最简单方法是将所有的setTimeout标识符存储在一个数组中,在这个数组中,您可以轻松地迭代到clearTimeout()。

var timeouts = [];
timeouts.push(setTimeout(function(){alert(1);}, 200));
timeouts.push(setTimeout(function(){alert(2);}, 300));
timeouts.push(setTimeout(function(){alert(3);}, 400));

for (var i=0; i<timeouts.length; i++) {
  clearTimeout(timeouts[i]);
}

#3


10  

I have an addition to Pumbaa80's answer that might be useful for someone developing for old IEs.

我在Pumbaa80的答案中增加了一项内容,可能对为老年人开发的人有用。

Yes, all major browsers implement timeout ids as consecutive integers (which is not required by specification). Althrough the starting number differs form browser to browser. It seems that Opera, Safari, Chrome and IE > 8 starts timeout ids from 1, Gecko-based browsers from 2 and IE <= 8 from some random number that is magically saved across tab refresh. You can discover it yourself.

是的,所有主要浏览器都将超时id实现为连续整数(这不是规范所要求的)。不同浏览器之间的起始数量不同。Opera、Safari、Chrome和IE > 8从1启动超时id,从2启动基于geck的浏览器,从某个随机数字IE <= 8启动超时id,然后通过tab刷新神奇地保存下来。你可以自己发现。

All that meens that in IE <=8 the while (lastTimeoutId--) cycle may run over 8digits times and show the "A script on this page is causing Internet Explorer to run slowly" message. So if you can not save all you timeout id's or don't want to override window.setTimeout you may consider saving the first timeout id on a page and clearing timeouts until it.

所有这一切意味着在IE <=8的while (lastTimeoutId——)周期可能超过8位数,并显示“这个页面上的一个脚本导致Internet Explorer运行缓慢”消息。如果不能保存所有超时id或不想重写窗口。setTimeout您可以考虑保存页面上的第一个超时id并清除超时,直到它发生。

Execute the code on early page load:

在页面加载的早期执行代码:

var clearAllTimeouts = (function () {
    var noop = function () {},
        firstId = window.setTimeout(noop, 0);
    return function () {
        var lastId = window.setTimeout(noop, 0);
        console.log('Removing', lastId - firstId, 'timeout handlers');
        while (firstId != lastId)
            window.clearTimeout(++firstId);
    };
});

And then clear all pending timeouts that probably was set by foreign code so many times you want

然后清除所有可能是由外国代码设置的超时

#4


5  

How about store the timeout ids in a global array, and define a method to delegate the function call to the window's.

在全局数组中存储超时id,并定义一个方法来将函数调用委托给窗口。

GLOBAL={
    timeouts : [],//global timeout id arrays
    setTimeout : function(code,number){
        this.timeouts.push(setTimeout(code,number));
    },
    clearAllTimeout :function(){
        for (var i=0; i<this.timeouts.length; i++) {
            window.clearTimeout(this.timeouts[i]); // clear all the timeouts
        }
        this.timeouts= [];//empty the id array
    }
};

#5


1  

Use a global timeout which all of your other functions derive timing from. This will make everything run faster, and be easier to manage, although it will add some abstraction to your code.

使用全局超时值,所有其他函数都从该超时值派生计时。这将使一切运行得更快,也更容易管理,尽管它将为代码添加一些抽象。

#6


1  

We've just published a package solving this exact issue.

我们刚刚发布了一个解决这个问题的软件包。

npm install time-events-manager

npm安装time-events-manager

With that, you can view all timeouts and intervals via timeoutCollection & intervalCollection objects. There's also a removeAll function which clears all timeouts/intervals both from the collection and the browser.

这样,您可以通过timeoutCollection & intervalCollection对象查看所有超时和间隔。还有一个removeAll函数,它清除集合和浏览器的所有超时/间隔。