每天减少一次变量 - Javascript

Soo*_*ran 7 javascript

我试图每天减少一次变量.我为此编写了以下代码.

var counter = 10; //any value

setInterval(function() {

  counter = counter - 1;

}, 86400000);
Run Code Online (Sandbox Code Playgroud)

是否有更好或更有效的方法来实现同样的目标?

PS: - 我不想使用任何库.

Mar*_*coS 2

我发现你唯一错过的是设置counter变量的初始值。
我会写:

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
Run Code Online (Sandbox Code Playgroud)