Non è possibile ottenere direttamente i secondi rimanenti del timer. È possibile salvare in una variabile il timestamp quando viene creato il timer e utilizzarlo per calcolare il tempo per l'esecuzione successiva.
Esempio:
var startTimeMS = 0; // EPOCH Time of event count started
var timerId; // Current timer handler
var timerStep=5000; // Time beetwen calls
// This function starts the timer
function startTimer(){
startTimeMS = (new Date()).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 time
function eventRaised(){
alert('WOP EVENT RAISED!');
clearTimer(timerId); // clear timer
startTimer(); // do again
}
// Gets the number of ms remaining to execute the eventRaised Function
function getRemainingTime(){
return timerStep - ((new Date()).getTime() - startTimeMS);
}
- Questo è il codice di esempio personalizzato creato "al volo".