Skip to content

Instantly share code, notes, and snippets.

@kerimdzhanov
Last active October 16, 2018 14:26
Embed
What would you like to do?
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