<div id="test"></div> <script> function delay(duration) { return new Promise(resolve => { setTimeout(resolve, duration); }); } (async () => { let counter = 0; while(true) { await delay(500); test.textContent = counter++; } })(); </script>