如何在JavaScript函数中使用回调函数 [英] How to use callback function in JavaScript functions
问题描述
我对JavaScript非常新,需要在我的java脚本函数中使用回调函数。我不知道如何使用回调函数。下面是我的代码:
I am very new to JavaScript and need to use callback function in my java script function. I don't know how to use a callback function. Below is my code:
function SelectedFeature() {
// Here is my code call_Method1();
call_Method2();
}
上述函数的问题是, call_method2 ()
在 call_Method1()
结束执行之前开始执行。为了解决这个问题,有人告诉我使用一个回调函数。现在如何在我的 SelectedFeature()
函数中使用回调函数?请使用代码示例解释。
The problem in the above function is that, call_method2()
starts executing before call_Method1()
ends its execution. To solve this problem, someone told me to use a callback function. Now how can I use callback function in my SelectedFeature()
function? Please explain by using code sample.
我在 call_method1()
中发出异步请求。我需要 call_Method2()
应该在完成执行后调用 call_method1()
。但是在我的情况下, call_method1()
完成其执行之前, call_method2()
现在我该如何解决这个问题?
I'm making an asynchronous request in call_method1()
. I need call_Method2()
should be called after completing execution call_method1()
. But in my case, call_method2()
calls before call_method1()
completes its execution. Now how can I fix this?
推荐答案
您使用什么来执行异步调用?你自己编写代码还是使用JQuery等库?
What are you using to do your asynchronous call? Did you code it yourself or are you using a library like JQuery?
你可以简单地把一个bool说成工作,你设置为true,当方法1开始,并返回到false,当它完成。你可以有method2等待,而工作是真的。
You could simply put a bool to say "working" that you set to true as method 1 starts and back to false when it finishes. you could then have method2 wait while working is true.
这篇关于如何在JavaScript函数中使用回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!