Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!