Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active Sep 24, 2021
Embed
What would you like to do?
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