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

341
Visualizações
How to set cursor to 'wait' for duration of script running?

I want to set the cursor style to 'wait' while a lengthy javascript function is running, and then reset back to normal afterwards.

I thought this might be enough:

document.body.style.cursor = 'wait';
// long scripting here
document.body.style.cursor = 'auto';

However .. if I move the cursor to point at (e.g.) an <input> then the cursor changes to the appropriate type e.g. 'text'. (Tested via individual commands in the console).

I did find this similar question, which sets up a css rule of
html.wait, html.wait * { cursor: wait !important; }
and uses that via this js:

document.querySelector("html").classList.add("wait");
// long scripting here
document.querySelector("html").classList.remove("wait");

.. but .. it doesn't seem to kick in while the script is running.

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

0

Put an idle delay where JS can release the CPU back to the browser so it gets a chance to do a repaint (or whatever it's called).

const DELAY_FOR_REFRESH = 25; // milliseconds
document.querySelector("html").classList.add("wait");
setTimeout( () => {
  // long scripting here
  document.querySelector("html").classList.remove("wait");
}, DELAY_FOR_REFRESH);

In limited testing, the DELAY_FOR_REFRESH needed to be more than at least 20ms — any quicker and the observed behaviour is that the browser doesn't repaint/update before the long scripting gets started.

NB. the reset of cursor back to 'auto' needs to happen at the end of the setTimeout() functionality (not after it).


My mental model of what happens here is something like this:

  1. DOM is updated with change to set the 'wait' cursor
  2. Browser hasn't updated the window yet because browser has'nt had a spare CPU cycle
  3. JS schedules the work for later
  4. CPU idles, waiting until the scheduled task
  5. Browser has idle CPU now, and enough time elapses it decides to do a repaint/whatever for the change in DOM from step 1
  6. the scheduled task kicks off
  7. the scheduled task eventually finishes, including resetting the cursor
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