Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active September 24, 2021 00:44
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 greggman/38d3eae518ad6220ccba00b1ca746cba to your computer and use it in GitHub Desktop.
Save greggman/38d3eae518ad6220ccba00b1ca746cba to your computer and use it in GitHub Desktop.
async loop with setTimeout based pause
/*bug-in-github-api-content-can-not-be-empty*/
<pre id="count"></pre>
const pause = _ => new Promise(resolve => setTimeout(resolve, _));
(async function() {
const countElem = document.querySelector('#count');
const start = performance.now();
const limit = 1000;
for (let i = 0; i < limit; ++i) {
countElem.textContent = `${i + 1} / ${limit}`;
await pause();
}
const end = performance.now();
console.log(`elapsed time: ${((end - start) / 1000).toFixed(2)} seconds`);
})();
{"name":"async loop with setTimeout based pause","settings":{},"filenames":["index.html","index.css","index.js"]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment