Skip to content

Runs a JavaScript callback with setInterval, at the top of every minute, with no drift.

License

Notifications You must be signed in to change notification settings

scripting/everyMinute

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

runEveryMinute

Runs a JavaScript callback with setInterval, at the top of every minute, with no drift.

No drift scheduling in JS

A well-known JavaScript problem, solved.

Some functions in servers and browser software need to run periodiically, say once a minute. If you code it in a straightforward way you get drift. The scheduling in JS is imperfect, and always errs on the side of running the code later than you requested, so after a few days it's running at 3 seconds into the minute, then 22 seconds, and so on. It becomes kind of a random number. I prefer everyMinute code to run at the top of the minute, always.

It's simple. It wakes up every second and sees if it's the top of the minute. If so it runs the callback. It has a backup plan, if it's been more than a minute since it ran the callback, it runs it now, and starts again at the top of the next minute

I tried more mathematical solutions, but they didn't work as well as this method. :-)

About

Runs a JavaScript callback with setInterval, at the top of every minute, with no drift.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published