如何让一个函数只在其调用的另一个函数完成后才能继续。事情是这样的:使一个函数等待/“暂停”直到另一个完成
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;
}
只要'FunctionTwo'没有任何异步,代码将执行你所希望的方式,你有它的方式...... – Ian
是的,这是Javascript的工作原理。你试过了吗?你有没有想要解决的具体问题?或者,也许你正在做一些异步?那么这可能是“从AJAX调用的函数返回值”问题的一个副本,我们已经拥有超过我们可以关闭的重复:)。 – kapa
是的,它有一个异步,我要求从一个API的一些数据,而我正在请求它,而使用第二个功能,我想要返回一个函数。唯一的问题是我需要发送更多的请求,但我需要等到从第二个函数返回结果(我在for循环中有第二个函数) – banzsolt