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

183
Views
EventListener execute order

I have simple code like follow:

setInterval(() => {
  window.scrollTo(x, y);
  // some simple logic (like console.log) goes here
},1000);
window.addEventListener('scroll', () => {
  console.log('scroll');
});

As i expect, event handler execute immediately after window.scrollTo(), but sometimes "some simple logic" execute earlier than scroll handler. Why?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The order of execution is not as it is in the code (chronologically). The event listener is triggered on the scroll event (part of the JS even loop mechanism) and the setInterval async function executes each second. JS does not support multi-threading, so I guess the processes are ordered in some kind of queue, and one of the executions (setInterval function executes on a strict time interval, whereas the eventListener executes when it has been triggered).

Maybe try reconstruct the logic to avoid this effect You can use this as a reference here

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