Skip to content
Advertisement

Changing Values in a Form Causes Lag

I want to change the value of a from once a second, but when i use this code:

JavaScript

The tab completely locks up, and I am forced to reload it. I have also tried using document.getElementById("input").value = "value";, but that causes the same thing to happen. Does anyone know how to fix this (the form does not have a name)?

Advertisement

Answer

The problem is that you have a classic “busy loop” that locks up the browser in executing Javascript code, without giving the browser a chance to do anything else like update the UI or respond to user actions.

Your while true loop, with no break statement inside, simply runs forever. Each time it runs, you put a Promise there – the result of sleep(1000).then(...), but the resolution of that Promise never gets a chance to be observed. The .then doesn’t happen automatically after one second, regardless of what else is happening – it can only happen when the browser event loop schedules it to run, which it can’t ever do here because you’ve got synchronous (blocking) code that never ends, but which must be run first.

If, as it seems, you want the effect to run every second, don’t bother with a loop and just use setInterval rather than setTimeout. You don’t even want to use Promises here as Promises can only resolve once while setInterval runs its handler continually. So all you need is something like this:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement