3
Just one example:
<script>
for (var i=0;i<10;i++) { //Inicia loop for para contar de 0 até 9
var t=0; // inicia variável t com valor 0
while(t<1000000) { //inicia loop enquanto t for menor que 1000000
t++; //Incrementa o t, ou seja, t = t + 1
} // Encerra o loop while, que neste caso não faz nada apenas cria um delay, mantendo o processador ocupado com a contagem, propositadamente!
document.getElementById('mensagem').innerHTML = `teste msg na tela ${i}` // Envia uma mensagem para a tela através de uma DIV por exemplo <<<aqui não funciona em tempo real>>>
console.log(`teste msg na tela ${i}`)//Envia mensagem para o console <<<aqui está funcionano em tempo real>>>
} // finaliza o loop for, se for menor que 10 volta para a linha "var t=0"
</script>
What I expected was to send a message on the screen at each "delay", but this does not happen, it only displays the 10 messages on the screen when the loop ends. Does anyone there know any commands to force the display of the text at the exact moment it is sent, within the loop? I intend to use this to update the Progress bar of a JS function that handles files and often takes time to finish, the user thinks that the browser has locked and closes the window before the function ends. If anyone can help, I’d be grateful! Obs. I tried with setTimeout, some artificials to force the Rendering, anyway, for now nothing worked, remembering that the above code is just a simple example to illustrate my problem.
What are you looking for, something like a Promise? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
– Mateus Daniel
Ever tried to use
setInterval
?– Sam
I tried, but it didn’t work either. I don’t have much experience with JS, but it seems to me to be a conceptual problem of language, while running a function does nothing else until it finishes that function. In my head a time interruption (setInterval or setTimeOut) should have priority in the execution, but in practice it seems that it does not happen...
– Veronezi
After you fire the
setInterval
, it keeps running regardless of whether other things are being executed in the script. From what I could understand, it would be enough to fire asetInterval
showing something on the screen, and when your preview is finished, just cancel thesetInterval
.– Sam
If I change "Document.get..." by "console.log" I can monitor the way I want in real time by the console monitor, then it works perfectly. But I need to do it through the browser screen.
– Veronezi
I’ve done this test with setInterval, when it enters the loop, stops updating the screen and only returns when the loop ends.
– Veronezi
Ever tried to put
+=
in place of=
after thedocument.get...
?– Sam
The fact is that under normal conditions you should not make the code this way in JS (nor in any language practically). Even if there was what you want, still a code that consumes all processing can be considered as having a bug if that is not a specific goal of it. Even codes that run 24/7 usually have a point where they release the CPU, except in case of intense calculations and things like that (such as a machine dedicated to finding prime numbers or mining of alguisacoins)
– Bacco