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

128
Vistas
Triggering synced CSS animations with Javascript by setting a timeStabilizer function

I want to trigger a simple blinking animation for the following buttons such as.

<style>
@keyframes buttonBlink {
    0% { opacity: 0;}
    100% { opacity: 1; }
}
button {
   border-radius: 25%;
   animation: buttonBlink 1s steps(2, start) infinite running;
}
.pressed {
  background-color: lightblue;
  outline: solid 2px black;
}

</style>
<body>
<div>
  <button id="button1" class="">button 1</button>
  <button id="button2" class="">button 2</button>
  <button id="button3" class="">button 3</button>
</div>
</body>
<script>
    document.querySelectorAll('button').forEach( i => {
        i.addEventListener('click', () => {
            i.classList.toggle('pressed');
        });
    });
</script>
 

The problem is that when pressed any button, they will start blinking at their own phase (they will not be in sync). So for that, I have refined the a little bit the expression by adding a function that calculates the difference to the second split in what I called timeStabilizer() (in this case secondStabilizer())

// function: secondStabilizer . It will execute the executeFunction delayed to
// the next second split

function secondStabilizer(executeFunction) {
    let initTime = new Date();
    initTime = initTime.getTime();
    nextSplitSecond = function nextSplitSecond(inputTimeSign) { 
        return (Math.ceil(inputTimeSign/1000) * 1000);
    }
    delayNeeded = nextSplitSecond(initTime) - initTime;
    setTimeout(executeFunction , delayNeeded);
}
// eof eof eof eof secondStabilizer eof eof eof eof eof

The function works when passing simple console.log functions as an argument, but embedding with the code for the toggle of the CSS class, it won't work

document.querySelectorAll('button').forEach( i => {
    i.addEventListener('click', secondStabilizer( () => {
         i.classList.toggle('pressed');
    }));
});

Is the approach good? Why is not working in my case?

about 4 years ago · Juan Pablo Isaza
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