【问题标题】:Add more seconds to countdown timer为倒数计时器添加更多秒数
【发布时间】: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


    【解决方案1】:

    这是您倒计时的另一个代码:

    timeout = null;
    time = null;
    startCountdown(5, 1000, end);
    
    // To add time
    $('#add').on('click', function(){
        time = time+5;
        startCountdown(time, 1000, end);
    });
    
    function startCountdown(timen, pause, callback) {
        time = timen;
        $('#timerdiv').html(timen);
        if(timen == 0)
            callback();
        else
        {
            clearTimeout(timeout);
            timeout = setTimeout(function(){
                startCountdown(timen-1, pause, callback)
            }, pause);
        }
    }
    
    function end() {
       alert();
    }
    

    http://jsfiddle.net/Lb4dK/2/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多