W
W
westdp2015-10-16 13:34:55
JavaScript
westdp, 2015-10-16 13:34:55

How to overwrite a variable every day (after 24 hours)?

On the site I use the plugin to animate counters countUp.js
Here is the code that I use on the site

var options = {
  useEasing : true, 
  useGrouping : true, 
  separator : '', 
  decimal : '.', 
  prefix : '', 
  suffix : '' 
};
var developer_users = 75000;
var developer = new CountUp("developer", 0, developer_users, 0, 6.5, options);
var talent = new CountUp("talent", 0, 6, 0, 5, options);
var growth = new CountUp("growth", 0, 25, 0, 4, options);
var colleges = new CountUp("colleges", 0, 800, 0, 6, options);
developer.start();
talent.start();
growth.start();
colleges.start();


How can I make the developer_users variable increase every 24 hours, for example, by 3 and be, for example, 75003, after another 24 hours - 75006, and so on, and when the page is refreshed, the counter is not reset.
Here you can see a working example - outsoft.com
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton B, 2015-10-16
@westdp

var developer_users = function() {

        var start = new Date('2015.10.10');
        var now = new Date();
        var days = Math.floor(((now - start) / 86400000));

        return 75000 + days * 3;
    }();

D
Denis Ineshin, 2015-10-16
@IonDen

To prevent the counter from being reset when updating, you need LocalStorage
To update the counter every 24 hours, you need to do something like this script, which, for example, will check the date every second

setInterval(function () {
    var now = new Date();
    var hour = now.getHours();
    var min = getMinutes();
    // а дальше сравниваем с этолоном, например с 0 часов 0 минут 0 секунд
    // обновляем счетчик и сохраняем его в localStorage
}, 1000)

UPD: Anton B 's solution is better.

D
Dasha Tsiklauri, 2015-10-16
@dasha_programmist

1) store on the server and transfer from the server
2) date function: f(x) = (dateNow-x).getDaysTotal()*3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question