【发布时间】:2011-04-11 15:23:48
【问题描述】:
我正在尝试使用setTimeout() 作为在 JS 中暂停一系列事件的一种方式。
这是我正在做的以及我想在 cmets 中做的一个例子 - http://jsfiddle.net/8m5Ww/2/
有什么建议可以用var a 中剩余的总毫秒数填充var timeRemaining?
【问题讨论】:
标签: javascript
我正在尝试使用setTimeout() 作为在 JS 中暂停一系列事件的一种方式。
这是我正在做的以及我想在 cmets 中做的一个例子 - http://jsfiddle.net/8m5Ww/2/
有什么建议可以用var a 中剩余的总毫秒数填充var timeRemaining?
【问题讨论】:
标签: javascript
您无法直接获取计时器剩余秒数。 您可以将创建计时器时的时间戳保存在变量中,并使用它来计算下一次执行的时间。
示例:
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 );
}
【讨论】:
不可能,但是如果您将单独 var 的内容设置为您设置它的时间,您可以轻松地手动计算出来。
【讨论】: