Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  93] [ 6]  / answers: 1 / hits: 70109  / 12 Years ago, tue, october 23, 2012, 12:00:00
function test(){
var distance=null;
first();
second();
third();
alert(distance);//it shows null always because it take 2 second to complete.

}
function first(tolat, tolon, fromlat,fromlon){

// calulating road distance between two points on the map using any other distance caluculating apis.

distance=dis; // update the value of distance but it takes 2 second to complete.

}
function second(){}
function third(){}


i have this type of situation in my code, now many time function three is called before first and second complete execution and distance value is not updated.


More From » jquery

 Answers
3

Make use of callbacks:



function first(tolat, tolon, fromlat, fromlon, callback) {
if (typeof(callback) == 'function') {
callback(distance);
}
}

function second() { }
function third() { }

first(vartolat, vartolon, varfromlat, varfromlon, function(distance) {
alert(distance);
second();
third();
});

[#82401] Monday, October 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zachariaho

Total Points: 34
Total Questions: 87
Total Answers: 100

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;