Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
How to safely send message to a running function in javascript

I tried to work this code:

var foo=0
window.onmouseup=function(){
    foo=1
}
window.onmousedown=function(){
    while(foo==0);
    console.log("bar")
}

the "bar" is not shown and the browser (I use Edge) stuck there(unable to close the page), I had to use Ctrl+T and then Ctrl+W

I guess the problem is foo==0 is optimized, so it reads from the cache, but I don't know how to avoid it. Or are there other methods?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could use setInterval() and put the if statement and the rest of the code in there:

var foo = 0
window.onmouseup = function() {
  foo = 1
}
window.onmousedown = function() {
  var interval = setInterval(() => {
    if (foo !== 0) {
      clearInterval(interval);
      console.log("bar")
    }
  });
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Actually, I think the problem is that your while loop will just continue running until it "breaks", or ends the loop. However, foo will always be 1 and never 0 after mouseup, therefore the program gets stuck in the while loop forever, and no other tasks on the browser including the important ones get run.

TL:DR program stuck on while

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda