Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8836779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:34:20+00:00 2026-06-14T09:34:20+00:00

I know that the setInterval function is pretty much single threaded. But if we

  • 0

I know that the setInterval function is pretty much single threaded. But if we set the time to a very small value like 50 milliseconds:

setInterval(function()
{
  // do really heavy stuff
}, 50);

then the first generated event might not finish in time then I guess a second event is put into the browser’s queue and then third, fourth etc. So basically this means that the browser’s queue is getting bigger really fast and it’s clearly not a good thing( is it? )

So far I’ve come with these two solutions:

1) Wait for the first event to finish and then call a second one(by using timeout):

var func = function()
{
  setTimeout(function()
  {
    // do really heavy stuff and when it's finished call again
    func();
  }, 50)
}

2) Create a flag that monitors the event’s state:

var isIntervalInProcess = false;
setInterval(function()
{
  if ( isIntervalInProcess )
    return false;

  isIntervalInProcess = true;

  // do really heavy stuff

  isIntervalInProcess = false;

}, 50);

Are those best practices for getting rid of events overlaping?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:34:21+00:00Added an answer on June 14, 2026 at 9:34 am

    Are those best practices for getting rid of events overlapping?

    The first example you provided is a common technique used to avoid the pitfalls of setInterval. As far as “best practice” is concerned, you should choose the right technique for the right situation (yes, that’s unfortunately quite vague).

    It’s common to use brief intervals for short periods of time using setInterval for animation. In these cases, the callback should be streamlined for performance and easily cancelled. jQuery uses a delay of 15ms, and it works great.

    For longer-running functions, it’s probably better to break them down into a queue of asynchronous functions. Queuing the same function over and over is identical to your first setTimeout example.

    Your second example has the issue that items in the callback queue will never execute out of order. The second queued function will only be called after the first function has executed, at which point 2 or 3 more functions may be queued.

    There is, however a cached polling technique that would allow the main body of the callback to execute only when a state change has occurred:

    (function () {
        var temp;
        setInterval(function () {
            var val;
            val = getSomeValue(); //some fast data accessor
            if (val === temp) {
                return; //break out of the function early
            }
            ...code to execute when the state has changed...
            temp = val; //cache the last state
        }, shortDelay);
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are other questions just like this but my code just
I have a jQuery function that uses setInterval() to slowly type out a phrase--like
So I know that there are differences between setTimeout and setInterval , but consider
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that Java have its own garbage collection, but sometimes I want to
I know this can't be that difficult, but I swear I can't find a
Say someone (evil) has set us a timer with setInterval , but we don't
I know that there are many similiar questions already asked on stackoverflow, but still,
I know that if port 443 is open that means the remote host supports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.