Javascript函数等待另一个函数完成

javascript function wait until another function to finish

本文关键字:函数 等待 Javascript 另一个      更新时间:2023-09-26

我有两个javascript函数从android调用。经过长时间的调试会话后,我终于意识到问题是由于第二个函数在第一个函数完成之前被调用。我已经搜索了延迟等的例子,但它们都取决于另一个函数中的函数调用。

function FunctInit(someVarible){ //someVariable is sent from android, cannot call again from getResult
//init and fill screen
}
function getResult(){ //also getResult need to be called from android via button
//return some variables
}

如何强制getResult等待FuncInit?有一种方法来实现这一点,通过Javascript?

在我看来,延迟/承诺(如您所提到的)是一种方法,而不是使用超时。

下面是我刚刚写的一个示例,用来演示如何使用deferred/promises来实现它。

花点时间和延迟者玩一玩。一旦你真正理解了它们,执行异步任务就变得非常容易了。

希望这对你有帮助!

$(function(){
    function1().done(function(){
        // function1 is done, we can now call function2
        console.log('function1 is done!');
        function2().done(function(){
            //function2 is done
            console.log('function2 is done!');
        });
    });
});
function function1(){
    var dfrd1 = $.Deferred();
    var dfrd2= $.Deferred();
    setTimeout(function(){
        // doing async stuff
        console.log('task 1 in function1 is done!');
        dfrd1.resolve();
    }, 1000);
    setTimeout(function(){
        // doing more async stuff
        console.log('task 2 in function1 is done!');
        dfrd2.resolve();
    }, 750);
    return $.when(dfrd1, dfrd2).done(function(){
        console.log('both tasks in function1 are done');
        // Both asyncs tasks are done
    }).promise();
}
function function2(){
    var dfrd1 = $.Deferred();
    setTimeout(function(){
        // doing async stuff
        console.log('task 1 in function2 is done!');
        dfrd1.resolve();
    }, 2000);
    return dfrd1.promise();
}

我可以想到几种方法来做到这一点。

使用回调:

 function FunctInit(someVarible){
      //init and fill screen
      AndroidCallGetResult();  // Enables Android button.
 }
 function getResult(){ // Called from Android button only after button is enabled
      //return some variables
 }

使用超时(这可能是我的首选):

 var inited = false;
 function FunctInit(someVarible){
      //init and fill screen
      inited = true;
 }
 function getResult(){
      if (inited) {
           //return some variables
      } else {
           setTimeout(getResult, 250);
      }
 }

等待初始化发生:

 var inited = false;
 function FunctInit(someVarible){
      //init and fill screen
      inited = true;
 }
 function getResult(){
      var a = 1;
      do { a=1; }
      while(!inited);
      //return some variables
 }

下面的答案可以帮助解决这个问题和其他类似的情况,如同步AJAX调用-

工作示例

waitForMe().then(function(intentsArr){
  console.log('Finally, I can execute!!!');
},
function(err){
  console.log('This is error message.');
})
function waitForMe(){
    // Returns promise
    console.log('Inside waitForMe');
    return new Promise(function(resolve, reject){
        if(true){ // Try changing to 'false'
            setTimeout(function(){
                console.log('waitForMe''s function succeeded');
                resolve();
            }, 2500);
        }
        else{
            setTimeout(function(){
                console.log('waitForMe''s else block failed');
                resolve();
            }, 2500);
        }
    });
}