Last active
October 16, 2018 14:26
-
-
Save kerimdzhanov/917ddcb5c9370257877442033d48fa9e to your computer and use it in GitHub Desktop.
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