Javascript 有没有办法检查 var 是否正在使用 setInterval()?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2679141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:26:25  来源:igfitidea点击:

Is there a way to check if a var is using setInterval()?

javascript

提问by chadley

For instance, I am setting an interval like

例如,我正在设置一个间隔,如

timer = setInterval(fncName, 1000);

and if i go and do

如果我去做

clearInterval(timer);

it does clear the interval but is there a way to check that it cleared the interval? I've tried getting the value of it while it has an interval and when it doesn't but they both just seem to be numbers.

它确实清除了间隔,但有没有办法检查它是否清除了间隔?我试过在它有一个间隔时获取它的值,当它没有时,但它们似乎都只是数字。

回答by Casey Chu

There is no direct way to do what you are looking for. Instead, you could set timerto false every time you call clearInterval:

没有直接的方法可以做你正在寻找的东西。相反,您可以timer在每次调用时设置为 false clearInterval

// Start timer
var timer = setInterval(fncName, 1000);

// End timer
clearInterval(timer);
timer = false;

Now, timerwill either be false or have a value at a given time, so you can simply check with

现在,timer在给定的时间要么是假的要么有一个值,所以你可以简单地检查

if (timer)
    ...

If you want to encapsulate this in a class:

如果你想把它封装在一个类中:

function Interval(fn, time) {
    var timer = false;
    this.start = function () {
        if (!this.isRunning())
            timer = setInterval(fn, time);
    };
    this.stop = function () {
        clearInterval(timer);
        timer = false;
    };
    this.isRunning = function () {
        return timer !== false;
    };
}

var i = new Interval(fncName, 1000);
i.start();

if (i.isRunning())
    // ...

i.stop();

回答by bobince

The return values from setTimeoutand setIntervalare completely opaque values. You can't derive any meaning from them; the only use for them is to pass back to clearTimeoutand clearInterval.

来自setTimeout和的返回值setInterval是完全不透明的值。你无法从中获得任何意义;它们的唯一用途是传回clearTimeoutclearInterval

There is no function to test whether a value corresponds to an active timeout/interval, sorry! If you wanted a timer whose status you could check, you'd have to create your own wrapper functions that remembered what the set/clear state was.

没有测试一个值是否对应于活动超时/间隔的功能,抱歉!如果您想要一个可以检查其状态的计时器,则必须创建自己的包装函数来记住设置/清除状态是什么。

回答by bobince

I did this like below, My problem was solved. you should set the value like "false", when you clearTimeout the timer.

我这样做如下,我的问题解决了。当您 clearTimeout 计时器时,您应该将值设置为“false”。

var timeer=false;
----
----
if(timeer==false)
{
  starttimer();  
}
-----
-----
function starttimer()
{
  timeer_main=setInterval(activefunction, 1000);
  timeer=true;
}

function pausetimer()
{
  clearTimeout(timeer_main);
  timeer=false;
}

回答by yankee

You COULD override the setInterval method and add the capability to keep track of your intervals. Here is an untestet example to outline the idea. It will work on the current window only (if you have multiple, you could change this with the help of the prototype object) and this will only work if you override the functions BEFORE any functions that you care of keeping track about are registered:

您可以覆盖 setInterval 方法并添加跟踪间隔的功能。这是一个未测试的示例来概述这个想法。它仅适用于当前窗口(如果您有多个,您可以在原型对象的帮助下更改它),并且只有在注册您关心的任何函数之前覆盖这些函数时,它才会起作用:

var oldSetInterval = window.setInterval;
var oldClearInterval = window.clearInterval;
window.setInterval = function(func, time)
{
  var id = oldSetInterval(func, time);
  window.intervals.push(id);
  return id;
}
window.intervals = [];
window.clearInterval = function(id)
{
  for(int i = 0; i < window.setInterval.intervals; ++i)
    if (window.setInterval.intervals[i] == id)
    {
      window.setInterval.intervals.splice(i, 1);
    }
  oldClearInterval(id);
}
window.isIntervalRegistered(id)
{
  for(int i = 0; i < window.setInterval.intervals; ++i)
    if (window.setInterval.intervals[i] == func)
      return true;
  return false;
}



var i = 0;
var refreshLoop = setInterval(function(){
    i++;
}, 250);

if (isIntervalRegistered(refrshLoop)) alert('still registered');
else alert('not registered');
clearInterval(refreshLoop);
if (isIntervalRegistered(refrshLoop)) alert('still registered');
else alert('not registered');

回答by The1stEsonian

The solution to this problem: Create a global counter that is incremented within your code performed by setInterval. Then before you recall setInterval, test if the counter is STILL incrementing. If so, your setInterval is still active. If not, you're good to go.

此问题的解决方案:创建一个全局计数器,该计数器在由 setInterval 执行的代码中递增。然后在你回忆 setInterval 之前,测试计数器是否仍在增加。如果是这样,您的 setInterval 仍处于活动状态。如果没有,你很高兴去。