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

  • Home
  • SEARCH
  • 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 6729079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:14:45+00:00 2026-05-26T10:14:45+00:00

I would like to thread some Javascript code while both the main process and

  • 0

I would like to thread some Javascript code while both the main process and thread are free to update the browser UI.

For example:

function StartStuff() {
    StartThreadedCode();
    // do more work and update the UI while StartThreadedCode() does its work
}

function StartThreadedCode() {
    // do stuff (do work and update the UI)
}

Is it possible?

  • 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-26T10:14:46+00:00Added an answer on May 26, 2026 at 10:14 am

    There are two main ways to achieve “multithreading” in Javascript. The first way is a cross-browser solution, that would also work in older browsers, but is more complicated to implement.

    The idea behind it is that you give the UI some time to update every once in awhile. Since there’s no synchronous sleep function in Javascript, the only way to achieve this is to use setTimeout (or setInterval with a little bit more complicate logic) to delay the execution of every loop of your complex calculations. This would give the browser some time to update the UI between loops, giving the visual effect of multiple things happening simultaneously. A few ms should be more than enough for the UI to reflect the latest changes.

    It has it’s drawbacks of course, and can be quite difficult to implement if there are multiple actions the user might want to do while the background calculations are being performed. Also it can drastically slow down the whole background calculation, since it’s delayed a few ms now and then. In specific cases, however, it does the trick and performs well.

    The second option would be to use web workers, that are basically Javascript scripts running independently in the background, like a thread. It’s much easier to implement, you only have to worry about messaging between main code and background workers, so your whole application isn’t affected as much. You can read about using them from the link posted by Mic https://developer.mozilla.org/en/Using_web_workers. The greatest drawback of web workers is their support by browsers, which you can see at http://caniuse.com/#search=worker There’s no possible workaround for IE <9 or mobile browsers that truly simulate the effect, so there’s not much you can do about those browsers, but then again, the benefits of modern browsers might outweigh poor IE support. This, of course, depends on your application.

    Edit: Im not sure whether I explained the first concept clearly enough, so I decided to add a small example. The following code is functionally equivalent to:

    for (var counter = 0; counter < 10; counter++) {
      console.log(counter);
    }
    

    But instead of logging 0-9 in quick succession, it delays 1s before executing the next iteration of the loop.

    var counter = 0;
    
    // A single iteration of your calculation function
    // log the current value of counter as an example
    // then wait before doing the next iteration
    function printCounter() {
      console.log(counter);
      counter++;
      if (counter < 10)
        setTimeout(printCounter, 1000);
    }
    
    // Start the loop
    printCounter();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to start and code a thread manager for
My Goal I would like to have a main processing thread (non GUI), and
I suspect that RegSetValueEx is thread safe, but would like some confirmation from the
I would like to reuse some existing code in our code base that accepts
I would like to multi-thread an application, however one library i'm using is not
i would like to implement a thread framework using simple c and timers....can anyone
I would like to know if there is a way to test whenever thread
This question is based on this thread in Meta . I would like to
My action listener on a JComboBox invokes a thread. I would like the component
How would I go about this? I have 3 rows like so: ID THREAD

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.