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

124
Visualizações
problem with setting loading animation class during sync JS work

I'm working on visual version of my sorting algorithm and I would like to show to the user a loading animaton during the sorting itself by JS. But the problem is that JS behaves weird then. It executes the first line and the last line (2 x toggle) and the launches the execution of the alogrithm. But if I add setTimeout (2nd function) it works as it should. Could anyone, please, explain to me how it works ? I've already tried to do a lot of reasearch but to no avail... It's counter-intuitive - because in first example it behaves like JS was asynchronous (1 -> first line animation added and 2 -> the last line animation removed, 3 -> the sorting work) and in the second example after adding setTimeout as synchronous (1 -> first line animation added, 2 -> sorting work, 3 -> animation removed)

function sortArray() {
  mainWrapper.classList.toggle("spinner-hidden");
  arrayCopy = [...arrayToSort];
  const sortedArray = bearSort(arrayCopy);
  outputArray.textContent = sortedArray;
  mainWrapper.classList.toggle("spinner-hidden");
}

function sortArray() {
  mainWrapper.classList.toggle("spinner-hidden");
  arrayCopy = [...arrayToSort];
  setTimeout(() => {
    const sortedArray = bearSort(arrayCopy);
    outputArray.textContent = sortedArray;
    mainWrapper.classList.toggle("spinner-hidden");
  }, 0);
}
about 4 years ago · Juan Pablo Isaza
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