views:

319

answers:

5

Well, first I want to say I'm a bit new in the world of Internet dev.

Anyway, I'm trying to know if its possible to run two pieces of code in parallel using javascript.

What I really need is to call two methods that are in a remote server. I pass, for both, a callback function that will be executed soon the data I want is ready. As the server running these functions take a time to answer, I'm trying to find a way to call both methods at the same time without need to wait till the first finishes to call the second.

Does methods like setTimeout run concurrently, for example

setTimeout(func1, 0);
setTimeout(func2, 0);

...

function func1()
{
   webMethod1(function() {alert("function 1 returned"); } );
}

function func1()
{
   webMethod2(function() {alert("function 2 returned"); } );
}

Edited

I've just found this article that may be very cool for the realease of next browsers: Javascript web workers

+6  A: 

There is one single thread of execution in Javascript in normal WebBrowsers: your timer handlers will be called serially. Your approach using timers will work in the case you present.

There is a nice piece of documentation on timers by John Resig (author of the very popular jQuery javascript framework - if you are new to Web development, I would suggest you look it up).

Now, if you are referring to HTML5 based browsers, at some point, they should have threading support.

jldupont
+1. although while it's true that they will be called serially, i'd add that OP's use of `webMethod` implies that what he *really* wants to avoid is synchronously waiting for two different web requests, and that can be avoided through AJAX.
David Hedlund
@David: we could note that in Browser there can be usually at least 2 connections to the origin server active in parallel, making AJAX calls effectively a good vehicle for parallelization.
jldupont
Great article..
Andres
A: 

It depends on the JavaScript engine.

Daniel A. White
No. JavaScript is single threaded in all browsers, and the OP was asking about JavaScript running in browsers.
Tim Down
+2  A: 

Yes, that's exactly how web requests through AJAX work. No need to setTimeout to 0, you can just call them one by one, and make an AJAX request, and it'll be executed asynchronously, allowing you to pass a callback function to be invoked when the request completes.

The means of creating an AJAX request differs some depending on what browser you're running. If you're going to build something that depends considerably upon AJAX, and you want it to work across multiple browsers, you're best off with a library. Here's how it's done in jQuery, for instance:

$.ajax({ url: '/webrequesturl', success: function(result) {
    // this will be called upon a successful request
} });
$.ajax({ url: '/webrequest2url', success: function(result) {
    // this will be called upon a successful request
    // this may or may not be called before the above one, depending on how long it takes for the requests to finish.
} });
David Hedlund
+2  A: 

Well, JavaScript is single-threaded, the two timers will run sequentially one after the other, even if you don't notice it.

I would recommend you to give a look to the following article, it really explains how timers and asynchronous events work, it will also help you to understand the single-threaded nature of JavaScript:

And as an alternative you could give a look to WebWorkers, is a way to run scripts in separate background threads, but they are only supported by modern browsers.

CMS
A: 

What you are looking for is asynchronous client-server communication (keyword: async). Asynchronous functions return straight away, but the provided callback will be executed after the specified condition is satisfied.

So, if the function that sends a request to the server is asynchronous, this would let you send both requests to the server without waiting for one to respond.

Using setTimeout may work, as this will schedule both request-sending functions to be called. However, some browsers only run one thread of Javascript at a time, so the result would be that one of the scheduled functions would run and block (waiting for a reply) and the other scheduled function would wait until the first was done to start running.

It is advisable to use async support from your server communication library. For instance jQuery uses async by default.

Victor Nicollet