javascript Javascript等待函数响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3624456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:46:12  来源:igfitidea点击:

Javascript wait for function response

javascriptajax

提问by Russell

I have the following code:

我有以下代码:

myFunc();
bar();

myFunc() is making an ajax request

myFunc() 发出 ajax 请求

I don't want to execute bar() until myFunc()'s request has completed.

在 myFunc() 的请求完成之前,我不想执行 bar()。

I also do not want to move the call to bar() inside of myFunc.

我也不想将调用移动到 myFunc 中的 bar()。

possible?

可能的?

EDIT

编辑

Here is the code I ended up with:

这是我最终得到的代码:

var FOO = {
 init: function(blah)
 {
  // Callbacks to pass to the AJAX challenge data load
  var callbacks = {
   myFunc1: function(){ myFunc1(blah); },
   myFunc2: function(){ myFunc2(blah); },
  };

  this.bar(callbacks); // Load the challenge data and setup the game
 },
 bar: function(callbacks) { ..loop through and execute them.. }

};

采纳答案by Jerome WAGNER

In order to do what you are looking for, you must find a way for bar()to communicate with myFunc().

为了做你正在寻找的事情,你必须找到一种让bar()myFunc()通信的方法

When you say that you do not want to move the call to bar()inside myFunc()this can be interpreted in several ways.

当您说您不想在myFunc()内移动对bar()的调用时,这可以有多种解释。

For example, you could make bar()a parameter of myFunc()

例如,您可以将bar()作为myFunc()的参数

function bar() {
   // do what bar() does
}

function myFunc(callback) {
   // use callback in the onreadystatechange property of the xmlhtprequest object
}

myFunc(bar)

I hope this will help you

我希望这能帮到您

Jerome Wagner

杰罗姆·瓦格纳

回答by fantactuka

IF you can add a kind of a flag that will indicate that myFunc() has finished with its request you can do it, but this is not a good idea - best practice is to use callbacks.

如果您可以添加一种标志,表明 myFunc() 已完成其请求,您可以这样做,但这不是一个好主意 - 最佳做法是使用回调。

Try this code:

试试这个代码:

var successRequest = false;
myFunc(); // when get the response set successRequest to true

var interval = setInterval(function() {
    if (successRequest) {
        clearInterval(interval);
        bar();
    }
}, 100);

回答by Darin Dimitrov

No, not possible unless you use a synchronous request which is no longer AJAX and something I wouldn't recommend you doing as it will freeze the UI until the request completes and users hate having their browser freezing.

不,不可能,除非您使用不再是 AJAX 的同步请求,而且我不建议您这样做,因为它会冻结 UI,直到请求完成并且用户讨厌他们的浏览器冻结。

回答by Quentin

No, it is not possible.

不,这是不可能的。

You could achieve it with the side effect that you lock up the browser by making a SJAX request instead of an AJAX request, but that is a terrible idea.

您可以通过发出 SJAX 请求而不是 AJAX 请求来通过锁定浏览器的副作用来实现它,但这是一个糟糕的主意。

Learn to love event driven programming when you work in an event driven programming environment.

在事件驱动的编程环境中工作时,学习热爱事件驱动的编程。