循环中的简单倒数计时器?

时间:2013-10-03 05:53:51

标签: jquery loops timer

我有一个循环,我正在计算用户的估计完成时间。

基本上,我希望格式为00:00:00 (hours, minutes,seconds)

我坚持了几秒钟,我已经能够计算出有多少分钟和秒数,但实际上倒计时我不是很好......

DEMO: http://jsfiddle.net/ZjWkb/

我知道我的问题是秒被显示为60,但由于if语句没有倒计时,它只是将其设置为60直到它低于60.

我该怎么做才能解决这个问题?

我不希望任何人为我写下整件事,我只是卡住了所有:)

2 个答案:

答案 0 :(得分:1)

var count = 1500;
var callsPerSecond = 1;
var delay = 1000;

//count * delay equals 1500000 miliseconds
//1500000 miliseconds == 25 mins, no hours, yet hours are displaying?


function offsetLoop(i, counter, idsRemaining) {

    while (i < counter) {
        var minute = 0;
        var secondsRemaining = (idsRemaining * delay) / 1000;
        var leftSec = 0;
        var hours = 0;

        var remainder = secondsRemaining % 60;
        minute = parseInt(secondsRemaining / 60);


        if (minute > 60) {
        hours = minute % 60;
            minute = parseInt(minute / 60);
        }

        secondsRemaining = secondsRemaining - 1;
        leftSec = remainder;


        secondsRemaining - i;
        if (typeof minute == 'undefined') {
            minute = 0;
        }
        if (typeof hours == 'undefined') {
            minute = 0;
        }
        hours = addZero(hours);
        minute = addZero(minute);
        leftSec = addZero(leftSec);
        log(hours + ":" + minute + ':' + leftSec);
        i++;
        idsRemaining--;
        if (i % callsPerSecond == 0) {
            setTimeout(function () {
                offsetLoop(i, counter, idsRemaining);
            }, delay);
            break;
        }
    }
};

function addZero(num) {
    if (num < 10) {
        num = ('0' + num).slice(-2)
    }
    return num;
}

function log(text) {
    $('#log').html(text);
}

offsetLoop(0, count, count);

答案 1 :(得分:1)

更新了小提琴以包含小时数。现在检查。我编辑了你的小提琴。似乎工作。链接:http://jsfiddle.net/ZjWkb/7/

问题在于以下部分:

var minute = (secondsRemaining) / 60;
secondsRemaining = secondsRemaining % 60;