Skip to content Skip to sidebar Skip to footer

How To Find The Remaining Time Of A Settimeout()

Possible Duplicate: Javascript: find the time left in a setTimeout()? I'm trying to use setTimeout() as a way of pausing a series of events in JS. Here's an example of what I'm

Solution 1:

You can't get directly the timer remaining seconds. You can save in a variable the timestamp when the timer is created and use it to calculate the time to the next execution.

Sample:

var startTimeMS = 0;  // EPOCH Time of event count startedvar timerId;          // Current timer handlervar timerStep=5000;   // Time beetwen calls// This function starts the timerfunctionstartTimer(){
   startTimeMS = (newDate()).getTime();
   timerId = setTimeout("eventRaised",timerStep);
}


// This function raises the event when the time has reached and// Starts a new timer to execute the opeartio again in the defined timefunctioneventRaised(){

  alert('WOP EVENT RAISED!');

  clearTimer(timerId); // clear timerstartTimer(); // do again
}

// Gets the number of ms remaining to execute the eventRaised FunctionfunctiongetRemainingTime(){
    return  timerStep - ( (newDate()).getTime() - startTimeMS );
}
  • This is custom sample code created "on the fly".

Solution 2:

Not possible, but if you set the contents of a separate var to the time you set it, you can easily figure it out manually.

Post a Comment for "How To Find The Remaining Time Of A Settimeout()"