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

前端 未结 6 887
失恋的感觉
失恋的感觉 2020-12-12 16:30

For instance, I am setting an interval like

timer = setInterval(fncName, 1000);

and if i go and do

clearInterval(timer);


        
相关标签:
6条回答
  • 2020-12-12 16:44

    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.

    0 讨论(0)
  • 2020-12-12 16:47

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

    var timeer=false;
    ----
    ----
    if(timeer==false)
    {
      starttimer();  
    }
    -----
    -----
    function starttimer()
    {
      timeer_main=setInterval(activefunction, 1000);
      timeer=true;
    }
    
    function pausetimer()
    {
      clearTimeout(timeer_main);
      timeer=false;
    }
    
    0 讨论(0)
  • 2020-12-12 16:51

    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:

    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');
    
    0 讨论(0)
  • 2020-12-12 16:57

    The return values from setTimeout and setInterval are completely opaque values. You can't derive any meaning from them; the only use for them is to pass back to clearTimeout and clearInterval.

    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.

    0 讨论(0)
  • 2020-12-12 16:58

    Well you can do

    var interval = setInterval(function() {}, 1000);
    interval = clearInterval(interval);
    if (typeof interval === 'undefined'){
    ...
    }
    

    but what are you actually trying to do? clearInterval function is an always success function and it will always return undefined even if you call it with a NaN value, no error checking in there.

    0 讨论(0)
  • 2020-12-12 17:01

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

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

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

    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();
    
    0 讨论(0)
提交回复
热议问题