【发布时间】:2014-02-10 00:10:55
【问题描述】:
我试图弄清楚每次单击按钮/链接时如何为倒数计时器添加更多时间。我找到了一段有效的代码,但我不知道如何编辑此代码以增加倒计时的时间。
在我的 HTML 中,我有一个 id 为“timerDiv”的 div,这就是计时器在我的页面上显示的位置。
代码如下:
window.onload = function() {
/* set your parameters(
number to countdown from,
pause between counts in milliseconds,
function to execute when finished
)
*/
startCountDown(100, 1000, whenTimerEnds);
}
function startCountDown(i, p, f) {
// store parameters
var pause = p;
var fn = f;
// make reference to div
var countDownObj = document.getElementById("timerDiv");
if (countDownObj == null) {
// error
alert("div not found, check your id");
// bail
return;
}
countDownObj.count = function(i) {
// write out count
this.innerHTML = i;
if (i == 0) {
// execute function
fn();
// stop
return;
}
setTimeout(function() {
// repeat
countDownObj.count(i - 1);
},
pause
);
}
// set it going
countDownObj.count(i);
}
function whenTimerEnds() {
alert("hi alex");
}
【问题讨论】:
标签: jquery timer add countdown seconds