基于时间的不均匀计数定时器Jquery

时间:2012-08-21 23:23:18

标签: javascript jquery countdown

我正在寻找一个基于一天中的时间的jquery计数计时器,我正在努力寻找一个,所以我在这里写了我自己的。这是任何正在寻找的人。

目标是采用一天中的时间,计算当前时间当天的百分比,将其乘以一个目标总和并开始添加以创建一个实际的时间方案。我的计时器正在开始创建非稳态数据流的模拟,但您可以轻松地将其更改为稳定增长。

编号为#counter的元素,目标总数为3.5毫米。

1 个答案:

答案 0 :(得分:0)

var currentNumber;

$(document).ready(function (e) {
    var currDate = new Date();
    var mins = currDate.getMinutes();
    var hours = currDate.getHours();
    var seconds = currDate.getSeconds();
    var combined = (hours * 60 * 60) + (mins * 60) + seconds;
    var percentage = (combined / 90000);
    var startingPoint = Math.round(3500000 * percentage);
    currentNumber = startingPoint;

    $('#counter').html(formatNumber(startingPoint));

    setTimeout(function () {
        updateNumber(currentNumber)
    }, 100);
});

function updateNumber(startingPoint) {
    if (startingPoint % 58 === 0) {
        startingPoint += 5;
        currentNumber = startingPoint;
        $('#counter').html(formatNumber(currentNumber));
        setTimeout(function () {
            updateNumber(currentNumber)
        }, 850);
    } else if (startingPoint % 22 === 0) {
        startingPoint += 4;
        currentNumber = startingPoint;
        $('#counter').html(formatNumber(currentNumber));
        setTimeout(function () {
            updateNumber(currentNumber)
        }, 500);
    } else if (startingPoint % 6 === 0) {
        startingPoint += 1;
        currentNumber = startingPoint;
        $('#counter').html(formatNumber(currentNumber));
        setTimeout(function () {
            updateNumber(currentNumber)
        }, 75); 
    } else {        
        startingPoint += 5;
        currentNumber = startingPoint;
        $('#counter').html(formatNumber(currentNumber));
        setTimeout(function () {
            updateNumber(currentNumber)
        }, 250);
    }
}

function formatNumber(number) {
    return addCommas((number).toFixed(0));
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}