Skip to content

Instantly share code, notes, and snippets.

@kimaldis
Created January 19, 2019 13:19
Show Gist options
  • Save kimaldis/aeadff19a673633712b5392b6313fa2d to your computer and use it in GitHub Desktop.
Save kimaldis/aeadff19a673633712b5392b6313fa2d to your computer and use it in GitHub Desktop.
Javascript Sleep Function
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// useage, await sleep(2000):
//
async function demo() {
console.log('Taking a break...');
await sleep(2000);
console.log('Two second later');
}
// or:
//
sleep(500).then(() => {
// Do something after the sleep!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment