Executes a given `fn` at the beginning of the next second
/** | |
* 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