Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrement a variable once a day - Javascript

Tags:

javascript

I'm trying to decrement a variable once a day. I have written the following code for that.

var counter = 10; //any value

setInterval(function() {

  counter = counter - 1;

}, 86400000);

Is there a better or efficient way to achieve the same thing ?

P.S : - I do not wish to use any libraries.

like image 612
Sooraj Avatar asked Apr 28 '16 12:04

Sooraj


1 Answers

The only thing I see you miss is to set the initial value of counter variable.
I would write:

var counter = 1000; // or any useful value

setInterval(function() {
  --counter;
}, 24 * 60 * 60 * 1000); // this is more self-explanatory than 86400000, and, being evaluated just once, it will have a tiny effect on the performace of the script
like image 53
MarcoS Avatar answered Sep 19 '22 17:09

MarcoS