Creating spin numbers until reaching a common value

Helped me yesterday: 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 rolling total until the final figure is reached ...

those. We sent to 190 countries, is this number possible from 0 to 190 when the text is reached on the screen?

So he will rotate the numbers until he reaches 190, and then stop.

Any help would be appreciated :)

+4
source share
1 answer

If you want only animations from 0 to 190, you can use this.

Just connect one function, increasing the displayed 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>
Run codeHide result
+2

Source: https://habr.com/ru/post/1606565/


All Articles