nee*_*rti 16 javascript ajax jquery
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(){}
Run Code Online (Sandbox Code Playgroud)
我在我的代码中有这种情况,现在很多时间函数三在第一次和第二次完成执行之前被调用而距离值没有更新.
Ang*_*o A 17
利用回调:
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();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
62941 次 |
| 最近记录: |