tags:

views:

248

answers:

3

if i fire up 2 functions like this:

prepare_data();
read_data();

then i guess they are running at the same time. but as you can see function two is depending on the first one, so how can i make the latter one run after the first one is completed?

+6  A: 

Javascript, and most other languages, run code sequentially.

Therefore, they will not both run at the same time.

In fact, it is not possible to run two different pieces of Javascript code at the same time. However, in many other languages, it is possible using threads.

However, if they make AJAX calls, the corresponding server-side code called by both functions will run simultaneously.
To prevent that, you'll need to make the first function accept a callback parameter and pass it the second function.

SLaks
I'd just get rid of "and most other languages" there. Way too sweeping a remark....
Crescent Fresh
Do you disagree? Are there any languages which execute every statement in parallel?
SLaks
@SLaks: nice strawman comeback there ;) All I meant was the sentence is kind of non-specific and slightly mis-leading there. Your stuff about threads and callbacks is what I think you were getting at.
Crescent Fresh
This user obviously needs to learn more about the basics of programming; I was trying to help educate him.
SLaks
@SLaks: fair enough.
Crescent Fresh
@SLaks: I disagree! In multi treaded applications you can execute statements in parallel. In such a system this question is very valid. May be this user knows more about programming than he reveals :)
Square Rig Master
I'm well aware that it's possible; see my fourth sentence. However, I don't think there are any languages which have the behavior that he's expecting. (Where two ordinary sequential lines of code will run in parallel by default)
SLaks
It's possible to run two or more different pieces of JavaScript code in the same script at the same time using window.setTimeout(..) https://developer.mozilla.org/en/DOM/window.setTimeout and related functions.
John K
@jdk: no, `setTimeout` simply queues execution. JavaScript is single threaded (in the browser). By definition it cannot execute more than one statement at the exact same moment in time.
Crescent Fresh
@Crescent: Very true. It is a common misconception that `setTimeout` will escape the browser's UI thread; in reality, it **will not**. (And therefore, Javascript developers never need to worry about thread safety) http://stackoverflow.com/questions/1594077/jquery-synchronous-animation
SLaks
Note that Firefox 3.5 does support multi-threading: https://developer.mozilla.org/En/Using_web_workers, as does Google Gears: http://code.google.com/apis/gears/api_workerpool.html
SLaks
+1  A: 

I'm assuming the first call makes some kind of asynchronous call that the second relies upon because otherwise the two functions will execute sequentially.

If so you need to do the second in the callback from the first. For example, in jQuery:

function prepare_data() {
  $("#data").load("/load_data.php", function() {
    read_data();
  });
}

It's impossible to say how you should solve this without more information on whether you're using vanilla Javascript or a particular library. I'd strongly suggest using a library of some sort for AJAX however.

cletus
+1  A: 

@SLaks

In fact, it is not possible to run two different pieces of Javascript code at the same time. However, in many other languages, it is possible using threads.

Is this completely correct? cant you run javascript parallely using web workers ?

http://www.w3.org/TR/2009/WD-workers-20090423/

https://developer.mozilla.org/En/Using%5Fweb%5Fworkers

mays
yes, you beat me to it.
Dan Beam
In IE, it is. In Firefox, it isn't.
SLaks