如何让jQuery的等待一个函数来执行另一个函数之前完成? [英] How can I make jquery wait for one function to finish before executing another function?
本文介绍了如何让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(){}
我有这种情况在我的code,现在很多时间功能前三第一和第二完成执行和距离值不更新被调用。
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.
推荐答案
请利用回调的:
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();
});
这篇关于如何让jQuery的等待一个函数来执行另一个函数之前完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文