如何在站点上实现实时和持久数字计数器

时间:2021-04-13 14:29:30

I have a client who recycles cans. We're developing a new web site for them and they want a live "cans processed" counter on their site. The starting number (since they've been in operation for a while) will be around 50,000,000, and there's not an ending number.

我有一个回收罐头的客户。我们正在为他们开发一个新的网站,他们希望在他们的网站上有一个现场“罐头处理”柜台。起始编号(因为它们已经运行了一段时间)将大约为50,000,000,并且没有结束编号。

To get "process rate" we can average out the number of cans processed per year and get a time estimate such as "1 can per 10 seconds". But how would I implement a "persistent" counter, so that the counter doesn't start ticking off from a set value at page load/refresh?

为了获得“过程费率”,我们可以平均每年处理的罐数并获得时间估计,例如“每10秒1罐”。但是,我如何实现一个“持久”计数器,以便计数器不会在页面加载/刷新时从设定值开始计时?

I'm a beginner with javascript and jQuery, but I do understand programming concepts and fundamentals, and I can read a script and figure out what's going on. Any ideas or suggestions would be very appreciated.

我是javascript和jQuery的初学者,但我确实理解编程概念和基础知识,我可以阅读脚本并弄清楚发生了什么。任何想法或建议将非常感激。

2 个解决方案

#1


5  

Take the beginning of this year:

今年年初:

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

Calculate how much time has elapsed:

计算经过的时间:

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

Let's say your rate is 1 can per 10 seconds:

假设您的费率为每10秒1罐:

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

Calculate how many cans have been processed since the beginning of this year:

计算自今年年初以来处理的罐数:

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

So, a simple set up could 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/

An optimization could be to keep your update interval aligned with the known rate, but it is probably better to keep your presentation updates decoupled from the rate information.

优化可以是使更新间隔与已知速率保持一致,但最好将演示文稿更新与速率信息分离。

#2


1  

I think I'd get current epoch with

我想我会得到当前的时代

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

and then compute the numbers of cans from that using

然后根据使用的数量计算罐头的数量

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

#1


5  

Take the beginning of this year:

今年年初:

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

Calculate how much time has elapsed:

计算经过的时间:

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

Let's say your rate is 1 can per 10 seconds:

假设您的费率为每10秒1罐:

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

Calculate how many cans have been processed since the beginning of this year:

计算自今年年初以来处理的罐数:

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

So, a simple set up could 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/

An optimization could be to keep your update interval aligned with the known rate, but it is probably better to keep your presentation updates decoupled from the rate information.

优化可以是使更新间隔与已知速率保持一致,但最好将演示文稿更新与速率信息分离。

#2


1  

I think I'd get current epoch with

我想我会得到当前的时代

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

and then compute the numbers of cans from that using

然后根据使用的数量计算罐头的数量

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