Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mals14
Last active June 19, 2020 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mals14/ff1edf75155998f5d8dcdb4e44f2640e to your computer and use it in GitHub Desktop.
Save mals14/ff1edf75155998f5d8dcdb4e44f2640e to your computer and use it in GitHub Desktop.
sleep pause capability in javascript.md

sourced from: What is the JavaScript version of sleep()? - Stack Overflow Link

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
  console.log('Taking a break...');
  await sleep(2000);
  console.log('Two seconds later, showing sleep in a loop...');

  // Sleep in loop
  for (let i = 0; i < 5; i++) {
    if (i === 3)
      await sleep(2000);
    console.log(i);
  }
}

demo();

Also,

Sourced from: Editing sleep pause capability in javascript.md Link

function delay (ms){   return new Promise(resolve => setTimeout(resolve, s));  }

"async" working demo on: http://zarsoft.info/software/_Test/pause_javascript/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment