Can I see if a timer is still running?

后端 未结 6 1779
青春惊慌失措
青春惊慌失措 2020-12-02 19:34

Simple question here that I can\'t seem to find an answer for: Once a setTimeout is set, is there any way to see if it\'s still, well, set?

if (         


        
相关标签:
6条回答
  • 2020-12-02 20:06

    There isn't anyway to interact with the timer except to start it or stop it. I typically null the timer variable in the timeout handler rather than use a flag to indicate that the timer isn't running. There's a nice description on W3Schools about how the timer works. In their example they use a flag variable.

    The value you are seeing is a handle to the current timer, which is used when you clear (stop) it.

    0 讨论(0)
  • 2020-12-02 20:12

    There is no need to check for an existing timer, just execute clearTimeout before starting the timer.

    var timer;
    //..
    var startTimer = function() {
      clearTimeout(timer);
      timer = setTimeout(DoThis, 6000);
    }
    

    This will clear any timer before starting a new instance.

    0 讨论(0)
  • 2020-12-02 20:15

    What I do is:

    var timer = null;
    
    if (timer != null) {
      window.clearTimeout(timer); 
      timer = null;
    }
    else {
      timer = window.setTimeout(yourFunction, 0);
    }
    
    0 讨论(0)
  • 2020-12-02 20:19

    I know this is a necroposting but i think still people are looking for this.

    This is what i use: 3 variables:

    1. t for milliseconds since.. in Date Object for next target
    2. timerSys for the actual interval
    3. seconds threshold for milliseconds has been set

    next i have a function timer with 1 variable the function checks if variable is truly, if so he check if timer is already running and if this is the case than fills the global vars , if not truly, falsely, clears the interval and set global var timerSys to false;

    var t, timerSys, seconds;
    
    function timer(s) {
      if (s && typeof s === "number") {
        if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
          timerSys = setInterval(function() {
            sys();
          }, s);
          t = new Date().setMilliseconds(s);
          seconds = s;
        }
      } else {
        clearInterval(timerSys);
        timerSys = false;
      }
      return ((!timerSys) ? "0" : t)
    }
    
    function sys() {
      t = new Date().setMilliseconds(seconds);
    
    }
    

    Example I

    Now you can add a line to sys function:

    function sys() {
      t = new Date().setMilliseconds(seconds);
      console.log("Next execution: " + new Date(t));
    //this is also the place where you put functions & code needed to happen when interval is triggerd
    
    }
    

    And execute :

      timer(5000);
    

    Every 5 seconds in console:

      //output:: Next execution: Sun May 08 2016 11:01:05 GMT+0200 (Romance (zomertijd))
    

    Example II

    function sys() {
      t = new Date().setMilliseconds(seconds);
      console.log("Next execution: " + seconds/1000 + " seconds");
    
    }
    
    $(function() {
      timer(5000);
    });
    

    Every 5 seconds in console:

          //output:: Next execution: 5 seconds
    

    Example III

    var t, timerSys, seconds;
    
    function timer(s) {
      if (s && typeof s === "number") {
        if (typeof timerSys === "boolean" || typeof timerSys === "undefined") {
          timerSys = setInterval(function() {
            sys();
          }, s);
          t = new Date().setMilliseconds(s);
          seconds = s;
        }
      } else {
        clearInterval(timerSys);
        timerSys = false;
      }
      return ((!timerSys) ? "0" : t)
    }
    
    function sys() {
      t = new Date().setMilliseconds(seconds);
      console.log("Next execution: " + seconds / 1000 + " seconds");
    
    }
    
    $(function() {
      timer(5000);
    
      $("button").on("click", function() {
        $("span").text(t - new Date());
      })
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button>Freebeer</button>
    <span></span>

    Note this way you can go below 0

    0 讨论(0)
  • 2020-12-02 20:24

    Set another variable Timer_Started = true with your timer. And also change the variable to false when the timer function is called:

    // set 'Timer_Started' when you setTimeout
    var Timer_Started = true;
    var Timer = setTimeout(DoThis,60000);
    
    function DoThis(){
    
       // function DoThis actions 
       //note that timer is done.
       Timer_Started = false;
    
    }
    
    function Check_If_My_Timer_Is_Done(){
    
       if(Timer_Started){
          alert("The timer must still be running.");
       }else{
          alert("The timer is DONE.");
       }
    
    }
    
    0 讨论(0)
  • 2020-12-02 20:28

    I usually nullify the timer:

    var alarm = setTimeout(wakeUpOneHourLater, 3600000);
    function wakeUpOneHourLater() {
        alarm = null;    //stop alarm after sleeping for exactly one hour
    }
    //...
    if (!alarm) {
        console.log('Oops, waked up too early...Zzz...');
    }
    else {
        console.log('Slept for at least one hour!');
    }
    
    0 讨论(0)
提交回复
热议问题