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

90
Visualizações
`setInterval` gets paused in React Component effect hook when window prompter is shown
const TestComponent: React.FC = (props) => {
  const [count, setCount] = useState(100);
  useEffect(() => {
    const interval = setInterval(() => {
      if (count === 98) {
        prompt("helloworld?");
        clearInterval(interval);
      }
      setCount((count: number) => count - 1);
    }, 1000);
    return () => {
      clearInterval(interval);
    };
  }, [count]);

  return <div>{count}</div>;
};

This is my sample code.

I was hoping the interval function keeps running no matter prompter is shown or not.

But as question says, the component gets paused and timer gets stopped until the prompter is resolved by user input.

Is there a way to keep interval runs as well as the component keeps render a new state?

And more deeper, I am also curious about what causes this behavior.

I guessed it comes from something related single threaded aspect of javascript but as I said, it is just guessing.

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

0

The spec says that prompt must pause while waiting for the user's response:

https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-prompt-dev

The prompt(message, default) method steps are:

...

  1. Pause while waiting for the user's response.

https://html.spec.whatwg.org/multipage/webappapis.html#pause

While a user agent has a paused task, the corresponding event loop must not run further tasks, and any script in the currently running task must block. User agents should remain responsive to user input while paused, however, albeit in a reduced capacity since the event loop will not be doing anything.

I thought you could get around it by using an iframe, but apparently even that doesn't work:

window.onmessage = function(e) {
        console.log('done');
};

setInterval(() => console.log('waiting'), 1000)
<iframe style="height:0; width: 0; display: none" id="demo"
        srcdoc="<script>window.parent.postMessage(prompt('hello'), '*')</script>">
        
</iframe>

Which means, your only way around it, realistically, is to fake a prompt, something like Creating a suitable replacement for prompt

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