Skip to content

Instantly share code, notes, and snippets.

@kerimdzhanov
Last active October 16, 2018 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerimdzhanov/917ddcb5c9370257877442033d48fa9e to your computer and use it in GitHub Desktop.
Save kerimdzhanov/917ddcb5c9370257877442033d48fa9e to your computer and use it in GitHub Desktop.
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