简体   繁体   中英

Creating Spinning Numbers until total is reached

I had some help yesterday to achieve this: http://jsfiddle.net/hopkins_matt/513ng07d/ (Thanks to Matt Hopkins) -----

function numberWithCommas(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function timeSince() {
    var prevTime = new Date(2015,8,8,0,0);
    var thisTime = new Date();
    return (thisTime.getTime() - prevTime.getTime()) / 1000;
}

function parcelCount() {
    var secDiff = timeSince();

    var leftNum = document.getElementById("left");
    var midNum = document.getElementById("mid");
    var leftNumCount = Math.round(((76/60) * secDiff) + 40093794);
    var midNumCount = Math.round(((43/60) * secDiff) + 22874098);

    leftNum.innerHTML = numberWithCommas(leftNumCount);
    midNum.innerHTML = numberWithCommas(midNumCount);
}
parcelCount();
setInterval(parcelCount, 1000);

I also want to create a spinning total until the final figure is reached...

IE We have shipped to 190 countries, is it possible to spin this number from 0-190 when the text is reached on screen?

So it would spin numbers until it reached the 190, then stop.

Any help would be appreciated :)

If you want just an animation from 0 - 190 you can use this.

Just loop one function increasing your display variable.

 var totalShipped = 190; var shippedDisplay = 0; var shippedStep = totalShipped / (2 * 1000 / 100); // Animation duration 2 sec function animateShipped() { if (shippedDisplay > totalShipped) shippedDisplay = totalShipped; document.getElementById("shipped").innerHTML = Math.round(shippedDisplay); if (shippedDisplay < totalShipped) { shippedDisplay += shippedStep; setTimeout(animateShipped, 100); } } animateShipped(); 
 <h3>Shipped</h3> <span id="shipped"></span> 

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