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 6666067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:47:22+00:00 2026-05-26T02:47:22+00:00

Consider an application which periodically executes a background task via setTimeout / setInterval .

  • 0

Consider an application which periodically executes a background task via setTimeout / setInterval. What’s the ideal amount of work one function call should do?

Example:

myTask = function() {
  for (var i=0; i<chunkSize; i++) { ...work... }
}

setInterval(myTask, period);

If you increase chunkSize (and do more iterations per call), of course it will run longer. That means, the period will also have to increase and you can call the function less often. But I found that nevertheless, the bigger the chunks, the more work gets done. This is probably due to overhead of setTimeout / setInterval

The trade-off is: bigger chunk sizes make the application less reactive. This does not only concern user interaction, but also other intervals. Because as long as the function is running its iterations, no other interval can get triggered. Thus, big chunk sizes make other intervals less accurate, or (in the worst case) completely disable them in some browsers so they don’t get called anymore.

If you want to

  • maximize the amount of work getting done
  • minimize bad influence on other intervals and keep them accurate

what is the best way to achieve that? How do you determine a good chunkSize? Or should I monitor execution times in the loop and abort after a certain time? Or something else?

(btw, this is about plain old JavaScript, not HTML5 WebWorkers)

  • 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-05-26T02:47:23+00:00Added an answer on May 26, 2026 at 2:47 am

    If you can, you should use setTimeout instead of setInterval. setInterval will call the function after period whether the function from the previous interval is complete or not. With setTimeout, you can make sure the function is complete before it called again.

    (function myTask() {
      for (var i=0; i<chunkSize; i++) { ...work... }
      setTimeout( myTask, period );
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What do I have to consider in database design for a new application which
We have a .NET application which our customers consider too large for mass deployment
I am developing an application which uses list view with check-boxes,consider there are 10
I am developing a application which requires to invoke a class function which is
We have an existing application for which one of our DTO object has a
Which IoC to consider while developing ASP.NET Web application project and what are the
Consider a song is getting downloaded over my android device via any other application.
Consider simple Java application which should traverse files tree in a disc to find
Consider following assumptions: I have Java 5.0 Web Application for which I'm considering to
Let's consider this scenario: a freshly create windows form application in which I created

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.