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

136
Vistas
why element starts rotating only after timer ends

here's a html p element. When clicked:

  1. background-color is changed to red than yellow
  2. it starts to rotate using css transform, by setting the element class to refresh-start

This is done in the click handler onClickRefresh.

However changes are reflected only after the timer is called, ends its operation and return. timer waits 5 sec (5000ms).

I would expect to see the changes immediately, before timer is called. that is:

  1. rotation starts immediately
  2. color changes to red than yellow

this is not happening. How does it make sense?

function onClickRefresh(el){
    //jquery selector
    let id = "#" + el.id;
    
    //read class 
    const co = (id)=>{ return $(id).attr("class")}

    //current class
    console.log("class before:", co(id));
    
    //setting class - start rotating
    el.className = "refresh-start";
    
    console.log("class after:", co(id));

    //temp color    
    $(id).css("background-color","red");
    
    timer(5000);
    
    $(id).css("background-color","yellow");
}


function timer(ms){
    const d1 = new Date();
    let cont = true;
    while (cont){
        let d2 = new Date();

        cont = (d2-d1 >= ms ? false : true);
    }
}
@keyframes rotate {
    from {transform: rotate(0deg)}
    to {transform: rotate(360deg)}
}

.refresh-start {
    animation-name: rotate;
    animation-duration: 1.5s;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    animation-play-state: running;
}

.refresh-end{
animation-play-state: paused;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<p id="refreshNRList1" class="refresh-end" onclick="onClickRefresh(this)"/>
    text
    </p>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Your timer function is synchronous it blocks the next code until it is completely executed. try asynchronous timer

async function onClickRefresh(el) {
  //jquery selector
  let id = "#" + el.id;

  //read class
  const co = (id) => {
    return $(id).attr("class");
  };

  //current class
  console.log("class before:", co(id));

  //setting class - start rotating
  el.className = "refresh-start";

  console.log("class after:", co(id));

  //temp color
  $(id).css("background-color", "red");
  await timer(5000);
  $(id).css("background-color", "yellow");
}

async function timer(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
@keyframes rotate {
    from {transform: rotate(0deg)}
    to {transform: rotate(360deg)}
}

.refresh-start {
    animation-name: rotate;
    animation-duration: 1.5s;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    animation-play-state: running;
}

.refresh-end{
animation-play-state: paused;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<p id="refreshNRList1" class="refresh-end" onclick="onClickRefresh(this)"/>
    text
</p>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try using setTimeout

setTimeout(
  () => {    
    $(id).css("background-color","yellow");
  }, 
  5000
);

This code calls a function after 5000 milliseconds, that changes the background-color to yellow. The rest of your code is executed immediately.

function onClickRefresh(el){
    //jquery selector
    let id = "#" + el.id;
    
    //read class 
    const co = (id)=>{ return $(id).attr("class")}

    //current class
    console.log("class before:", co(id));
    
    //setting class - start rotating
    el.className = "refresh-start";
    
    console.log("class after:", co(id));

    //temp color    
    $(id).css("background-color","red");
    
    setTimeout(
      () => {    
        $(id).css("background-color","yellow");
        }, 
        5000
    );
}
@keyframes rotate {
    from {transform: rotate(0deg)}
    to {transform: rotate(360deg)}
}

.refresh-start {
    animation-name: rotate;
    animation-duration: 1.5s;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    animation-play-state: running;
}

.refresh-end{
animation-play-state: paused;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<p id="refreshNRList1" class="refresh-end" onclick="onClickRefresh(this)"/>
    text
    </p>

about 4 years ago · Juan Pablo Isaza Denunciar

0

TL;DR: Use setTimeout():

    //remove the "`timer(5000);`" line and the timer function.
    setTimeout(() => {
      $(id).css("background-color","yellow");
    }, 5000)

Your problem is in the timer function. HTML/JavaScript/CSS are single-threaded. It means, that if you make something in an infinite loop, everything will stop. You had some kind of infinite loop in your timer function, though it ended at the end on 5 seconds, but in the meanwhile, everything was 'lagged' and the CSS animation couldn't start. For this reason you have to use this syntax:

setTimeout(() => {
    //code...
}, 5000 /*delay in milliseconds*/)
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