Reduce a variable once a day - Javascript

I try to decrease the value of a variable once a day. I wrote the following code for this.

var counter = 10; //any value setInterval(function() { counter = counter - 1; }, 86400000); 

Is there a better or effective way to achieve the same?

PS: - I do not want to use any libraries.

+7
javascript
source share
6 answers

The only thing I see that you miss is setting the initial value of the 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 
+1
source share

I do not see any problems with the way you write it. You use the interval, alright, but this is not the worst evil you can do to adjust the value of a variable.

You might think of another solution with a function that returns the current counter to you.

 var initialValue = 20000; function getCounter() { return initialValue - Math.floor(Date.now() / 1000 / 60 / 60 / 24); } console.log(getCounter()); 

The difference is that it takes the current day number, starting from the start of UNIX. Every day, the number of days will be increased, so the result of the function will be reduced by 1.

But I don’t see how this solution can be better than yours.

+2
source share

I'm not quite sure why, but using setInterval like this makes me uncomfortable.

If I required this, I would use something like this:

 var counter = 10; var timeout = new Date(); setInterval(function(){ if(new Date() >= timeout) { --counter; // the action to perform timeout = new Date(timeout.getTime() + 86400000); // update the timeout to the next time you want the action performed } console.log(counter); },1000); // every second is probably way more frequent than necessary for this scenario but I think is a decent default in general 

One thing this permits is, for example, setting the next timeout at midnight tomorrow instead of being blocked until "X seconds after the previous execution." The key is the inversion of the control - the action itself can now dictate when it should be performed as follows.

Although, I probably could distract the details of the interface, taking in the beginning, interval and action.

0
source share

Perhaps use window.localStorage to save the last time, and if it is more than 60 * 60 * 24 (seconds per day), set the last time for this morning / now / 1: 00, and then decrease the value and save it.

Example:

 var d = new Date(); var mins = -(1+d.getHours())*60+d.getMinutes(); var secs = mins*60+d.getSeconds(); // total seconds passed today from 1:00 var now = d.getCurrentTime(): var lastCheck = localStorage.getItem("lastCheck"); if (!lastCheck) { localStorage.saveItem("lastCheck",now-secs); // beginning of today } var dayPassed = now - lastCheck > 24*60*60; // change to see if a day has passed if (dayPassed) { // save seconds localStorage.setItem("counter",localStorage.getItem("counter")-1); localStorage.saveItem("lastCheck",now-secs); // beginning of today } 
0
source share

The biggest problem in my eyes is that you have to constantly support this JS process for several days so that it does what you need. The world is not so perfect that it does not require a periodic reboot ... including the average JS process.

Personally, I would save the timestamp of my starting point, then (when I need to know how much time has passed), take a new timestamp and use it to calculate how many days have passed. That way, even if something interrupts my process, I can still be right where I started.

0
source share

I need to check more how many days have passed since a certain date and reduce the number of days of the counter. Mostly just because I did not expect anyone to leave the same page open unnecessarily or want to reload for several days in a row. I would do something like this:

 counter = 365; // original counter var start = new Date(2016, 03, 20); // original date var now = new Date(); var days = Math.floor(Math.abs(start.getTime()-now.getTime())/(24*60*60*1000)) counter -= days; 

Thus, every time you visit a page, it will decrease correctly. Please note that this ignores any problems with the temporal days or time zones. In the above example, there will be a 360 counter for me. And if you expected it to be open in a few days, restart it automatically:

 self.setTimeout(function(){document.location.reload()}, 86400000); 
-2
source share

All Articles