Srikanth
Srikanth

Reputation: 836

setInterval for longer hours

Is setInterval method reliable for longer hours? I have a requirement to trigger functions for once everyday and/or once everyweek. So am using setInterval(myFunc, 86400000) //For one day. similarly calculated number of milliseconds for one week Is this a good approach. or is there anyother technique i can use.

Upvotes: 5

Views: 3234

Answers (2)

Lemex
Lemex

Reputation: 3794

I think a good option would be,

when the web page opens first store the date to local storage, when its open normally check the date in local storage every minute and then validate it and take action.

setIterval sounds like a very bad way of doing it here...

On start up get the data,

save the date using localStorage.setItem("date", datevariblename);

on load or everytime you in the set interval use localStorage.getItem("date") to get the date value

Then do what every comparsion is required between current date and save date

Upvotes: 1

jAndy
jAndy

Reputation: 236122

setTimeout is pretty darn precise for longer periods. I had the requirement myself some time ago, so I did some testing across browsers. Even if you have a timeout that lasts 24 hours for instance, the callback gets fired precisely (variance is in the area of miliseconds).

setTimeout (or more exactly, the implementation) does not use a "countdown". It will ask the operating system to give it a SIGnal when the time period is over.

Upvotes: 0

Related Questions