Executes a given `fn` at the beginning of the next second
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Executes a given `fn` at the beginning of the next second. | |
* @param {function} fn – a function to execute | |
* @return {number} setTimeout's resulting timeout id, to give ability to cancel execution | |
*/ | |
function nextSecond(fn) { | |
const time = (new Date()).getTime(); | |
return setTimeout(fn, (Math.ceil(time / 1000) * 1000) - time); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment