[英]Call one function after another in function in Jquery
I have two functions ,function1() and function2() .我有两个函数,function1() 和 function2()。 Below is the structure of code下面是代码结构
function1(){
//Inside this function there is one more function(present in different JS file)
functionA(){ //functionA calls a different function in some other JS file
}
}
function2(){
}
I tried giving call back so function1() will be executed before function2().我尝试回拨,因此 function1() 将在 function2() 之前执行。 This didn't work.这没有用。
Can anyone suggest.任何人都可以建议。 How can I execute function1()(all the functions inside the function) and then function2()?如何执行 function1()(函数内的所有函数)然后执行 function2()?
Your question is vague but I'm assuming you're doing an async operation and it need to wait for a return something like this:你的问题很模糊,但我假设你正在做一个异步操作,它需要等待这样的返回:
function1(callback){
// last function called
functionA(callback){
// some ajax or async operation
$.ajax(function(){ }).done(callback)
}
}
function2() {};
function1(function2)
If however there are multiple ajax calls and you need it to be done last you will have to chain all of the calls together or you will have to make a synchronization mechanism to call once all of the ajax calls have returned.但是,如果有多个 ajax 调用并且您需要最后完成它,则必须将所有调用链接在一起,或者一旦所有 ajax 调用都返回,您将必须创建一个同步机制来调用。
You can return the second function and then call.您可以返回第二个函数,然后调用。
function fn1(){
return function fn2(){
}
}
fn1()()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.