Javascript 使函数等待/quot;暂停“;直到另一个完成
如何使一个函数仅在它调用的另一个函数完成后才继续。大概是这样的:Javascript 使函数等待/quot;暂停“;直到另一个完成,javascript,jquery,callback,Javascript,Jquery,Callback,如何使一个函数仅在它调用的另一个函数完成后才继续。大概是这样的: function FunctionOne() { //Do something ... FunctionTwo() //Rest of the code after two is finished } function FunctionTwo() { //Some code } function FunctionOne() { //Do something ... var result; for
function FunctionOne() {
//Do something ...
FunctionTwo()
//Rest of the code after two is finished
}
function FunctionTwo() {
//Some code
}
function FunctionOne() {
//Do something ...
var result;
for (var i = 0 , i < 100 , ++i){
result = FunctionTwo()
}
//Rest of the code after two is finished
console.dir(result); //this here gives me undefined
}
function FunctionTwo() {
$.get("url", function(data)
{
result = data;
console.dir(result); //this gives me the data but the first function is still running and it finishes faster then the data is retured
return result;
}
编辑:
确切的功能如下所示:
function FunctionOne() {
//Do something ...
FunctionTwo()
//Rest of the code after two is finished
}
function FunctionTwo() {
//Some code
}
function FunctionOne() {
//Do something ...
var result;
for (var i = 0 , i < 100 , ++i){
result = FunctionTwo()
}
//Rest of the code after two is finished
console.dir(result); //this here gives me undefined
}
function FunctionTwo() {
$.get("url", function(data)
{
result = data;
console.dir(result); //this gives me the data but the first function is still running and it finishes faster then the data is retured
return result;
}
FunctionOne()函数{
//做点什么。。。
var结果;
对于(变量i=0,i<100,+i){
结果=函数二()
}
//完成两个之后的其余代码
console.dir(result);//这里给出了未定义的
}
函数FunctionTwo(){
$.get(“url”),函数(数据)
{
结果=数据;
console.dir(result);//这会提供数据,但第一个函数仍在运行,它完成得比返回数据更快
返回结果;
}
这应该已经按照您的要求执行了。除非您指定FunctioTwo异步运行,否则它将为您阻塞
编辑以响应您的编辑:
如果希望Ajax调用阻塞,那么没有理由将其作为异步调用,如果确实希望它保持异步,那么利用Ajax调用的回调来执行数据处理函数,而不是将代码放在主块中
function FunctionOne() {
//Do something ...
var result;
for (var i = 0 , i < 100 , ++i){
result = AJAXCall(i)
}
}
function HandleAJAXResponse(response,id)
{
console.dir(response)
}
function AJAXCall(id) {
$.get("url",function(data,id){
HandleAJAXResponse(data,id);
});
}
FunctionOne()函数{
//做点什么。。。
var结果;
对于(变量i=0,i<100,+i){
结果=AJAXCall(i)
}
}
函数HandleAJAXResponse(响应,id)
{
console.dir(响应)
}
函数AJAXCall(id){
$.get(“url”,函数(数据,id){
Handleajax响应(数据,id);
});
}
您必须使用并设置async:false
一个简单的例子:
result = '';
function FunctionTwo() {
jQuery.ajax({
url: 'url',
success: function(data) {
result = data;
console.dir(result);
},
async: false
});
alert(result);
}
只要
FunctionTwo
没有任何异步功能,代码就会按您想要的方式执行,按您拥有的方式执行……是的,这就是Javascript的工作方式。您尝试过吗?您有没有试图解决的特定问题?或者您正在做的事情是异步的?那么对于“使用AJAX调用从函数返回值”这一系列问题,我们已经有了很多问题,无法作为一个重复来解决:)是的,它有一个异步,我从api请求一些数据,我在使用第二个函数时请求它,我想用一个函数返回它。唯一的问题是我需要发送更多的请求,但我需要等待,直到我从第二个函数得到结果(我在for循环中有第二个函数)@banzsolt然后请在下次提问时澄清。作为重复关闭。@banzsolt在ajax调用中将async设置为false,然后,如果您想等待它,则没有理由异步运行它。如何指定异步运行的函数?最简单的方法是通过设置为0的计时器触发它。@kapasetTimeout(myFunction,0);
@Wobbles我的论点是,您不能异步调用/运行函数。您可以指定稍后将由其他代码调用/运行的回调(事件处理程序,setTimeout
等)。@Wobbles要么我不明白您想说什么,要么Javascript函数不能立即运行(同步),也不能在之后阻止代码(异步)同时执行。