How to implement a permanent number counter on a site

I have a client who processes banks. We are developing a new website for them, and they want the counter processed by the banks to be processed on their website. The initial number (since they have been in operation for some time) will be about 50,000,000, and there will be no final number.

To get the “speed of the process”, we can average the number of cans processed per year and get an estimate of the time, for example, “1 in 10 seconds”. But how do I implement a “persistent” counter so that the counter does not start to cancel the set value when loading / updating the page?

I start with javascript and jQuery, but I understand the concepts of programming and the basics of programming, and I can read the script and find out what happens. Any ideas or suggestions would be greatly appreciated.

+3
source share
2 answers

Take the start of this year:

var start = new Date(2011, 0, 1); // Note the 0!

Calculate how much time has passed:

var elapsed = new Date() - start; // In milliseconds

Say your bet is $ 1 in 10 seconds:

var rate = 1 / 10 / 1000; // Cans per millisecond

Calculate how many containers have been handled since the beginning of this year:

var cans = Math.floor(elapsed * rate);

Thus, a simple setup can be:

var start = new Date(2011, 0, 1);
var rate = 1 / 10 / 1000;
var base = 50000000;

setInterval(function () {
    var cans = Math.floor((new Date() - start) * rate) + base;
    $("#cans").text(cans); // I hope you're using jQuery
}, 5000); // update every 5 seconds -- pick anything you like; won't affect result

http://jsfiddle.net/eKDXB/

Optimization may be to maintain the refresh interval according to a known speed, but it is probably best to keep your presentation updates untied from the speed information.

+4
source

I think I get the current era with

var msecs = (new Date()).getTime();

,

var total_number = base_number + (msecs - start_date) / msecs_per_can;
+1

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


All Articles