简体   繁体   中英

Uneven Countup Timer Jquery Based on Time of Day

I was looking for a jquery countup timer that was based on the time of day, and I was struggling to find one so here I just wrote my own. Here it is for anyone who's looking for one.

The objective is to take the time of day, calculate a percentage through the day of that current time, multiply it by an objective sum and start adding to create a realistic timing scenario. My timers are firing to create the simulation of an unsteady data flow, but you can change it to a steady increase easily.

The element with the number is #counter , and the objective total is the 3.5mm.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM