Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

89
Vistas
`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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda