Novemberborn, Straight lines circle sometime

JavaScript Threading: Quick Tip

JavaScript runs in a single thread, which is why a lot of things, including XHR, work with callbacks. But sometimes, sometimes you need to escape the thread [1] and run you code in a new one. One example is event based code, where your stuff should not run while the event is still being propagated. The easiest way to accomplish this is by using setTimeout():

setTimeout(myCode, 0);

Since JavaScript is single-threaded, and the timeout 0 milliseconds, the timer will fire when there is no JavaScript being executed. In theory it’s still possible for other threads to be started, blocking the timer, but this shouldn’t lead to any practical problems.

Happy coding!

[1]: For sake of argument. Technically it’s all the same thread, and execution would depend on queues and callstacks, but I find it easier to think of this as in different threads.

link | javascript | 2 August 2006, 06:30


Comments

  1. Wouldn’t this be a litle more accurate?

    setTimeout(myFunction, 0);
    

    Matthias Miller | 2 August 2006, 15:34 | link

  2. Oops! Thanks Matthias :)

    Mark Wubben | 2 August 2006, 16:18 | link

  3. good point. i came across this problem recently when i noticed Safari doesn’t like to execute “trigger mouse event” function from within an event handler. IE and FF seemed to be ok with it!

    boo | 6 August 2006, 06:48 | link

  4. probably a bad idea to assume an implementation of JS is single threaded, the key concept is that the setTimeout call is asynchronous…

    tim scarfe | 12 August 2006, 00:28 | link

  5. Great tip, thanks…. everyday there’s something new to learn when it comes to Js/Ajax.. :)

    Rasesh | 18 August 2006, 01:59 | link

  6. As you said, javascript is single threaded but there are ways around this that can help you create calls that run in the background, giving you the illusion of a threaded system. I suppose this is the other side of the coin from what you are suggesting above but certainly it’s an interesting concept. Have a look here : http://www.hwhappy.co.uk/2006/11/07/concurrent-xml-queries-with-ajax/

    A Gilmour | 8 November 2006, 00:09 | link

  7. Not quite. With asynchronous XHR, the actual request is made without blocking the JavaScript thread (as with synchronous XHR). Then, when the request finishes, a JavaScript method is invoked, which is single-threaded. Due to browser limitations there most likely won’t be more than two XHR requests to the same server at the same time, so it’s not even truly concurrent on the browser side of things.

    Mark Wubben | 8 November 2006, 09:45 | link

  8. umm, setTimeout(“fuction()”,0);

    that would just call the fuction fucntion() every 0 milliseconds right?

    how would that implement a thread, especially if you need to move an object over a cetain period of time?

    would you just put another function inside the function you’re calling every 0 milliseconds?

    for example:


    var time = 0;

    fcn1();

    function fcn1() {

    if(time < 1) { fcn2(); time++; }

      function fcn2()
      {
           x += 10;
           y += 2;
           setTimeout("fcn2()",400);
      }
    

    setTimeout(“fcn1()”,0);

    }

    is this a good example?

    even in this example, fcn2 is just goin to happen every 400 sec. so I dont see how setting the time recurrencey to 0 helps any.

    Justin Fox | 11 September 2007, 16:07 | link

  9. It’s not really threading, more scheduling a function to be executed in a new call stack. You’re looking for setInterval which is unrelated to this discussion.

    Mark Wubben | 12 September 2007, 12:37 | link

Leave your comment

Please keep it polite and on topic. Yes, your e-mail address is required, but it's kept private. HTML is not allowed in the comments but you can use Markdown. Non-contributing comments run the risk of being removed. Especially if the website seem “fishy”. Spammers, beware.

(required)
(required, kept private)
(optional, but let's share it!)
(required)

Remember my details


Novemberborn: Extra

About the author

Mark Wubben is a hacker/writer in Enschede, the Netherlands.

Read more about Mark...

Go to

Jobs (NL)

Xopus zoekt programmeurs! Verbeter de code en win!

Subscribe