简体   繁体   English

Javascript / jQuery make函数在继续之前等待回调

[英]Javascript / jQuery make function wait for callback before continuing

I need to know how to make a function wait for a callback from another function before executing. 我需要知道如何使一个函数在执行之前等待来自另一个函数的回调。 Below is a simplified scenario of what I am trying to accomplish. 下面是我要完成的任务的简化方案。

function a(){
  b();
  // Wait for function c to callback
  var callback = function();
  // then continue function a() logic...
}
function b(){
  c();
  return false;
}

function c(){
  //trigger callback in function a
    callback();
}

Put the rest of a's logic into the callback, and if you need, use a reference to a's this 将a的其余逻辑放入回调中,如果需要,请使用对this的引用

function a(){
  b();
  var that = this;
  var callback = function(){
    that.variables
    // then continue function a() logic...
  }
}
function b(){
  c();
  return false;
}

function c(){
  //trigger callback in function a
    callback();
}

What you need to do is to pass a callback to b and c as shown below and call the callback in c after its execution... assuming c is doing an async operation. 您需要做的是将回调传递给bc ,如下所示,并在执行后在c调用该回调...假设c正在执行异步操作。

 function a() { snippet.log('inside a'); b(function() { //code that should be executed after `c` must be here... note that you cannot return a value from here to the caller of `a` snippet.log('inside callback'); }); snippet.log('after b') } function b(callback) { snippet.log('inside b'); c(callback); return false; } function c(callback) { snippet.log('inside c'); setTimeout(function() { snippet.log('inside async timeout'); //trigger callback in function a callback(); }, 100); } a(); 
 <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 让 function 在继续之前等待另一个 function - Javascript make function wait for another function before continuing Jquery 等待 Function 的返回值后再继续 - Jquery Wait for Return Value of Function before Continuing Selenium - 在继续之前等待Javascript函数执行 - Selenium - Wait for Javascript function to execute before continuing 等待函数完成,然后再继续使用JavaScript - Wait for a function to finish before continuing in javascript 在 Javascript Cordova iOS 中继续循环之前如何等待回调结果? - How to wait for callback result before continuing the loop in Javascript Cordova iOS? 在继续 for 循环之前等待 HTTP 请求中的回调 function 完成 - Wait for completion of callback function in HTTP request before continuing with for loop 我如何让我的 javascript 函数在继续之前等待 WebSql 事务完成 - How i make my javascript function wait to WebSql transactions to finish before continuing 如何让程序在继续使用javascript之前等待if语句完成? - How to make the program wait for the if statement to finish before continuing in javascript? 在Javascript JQuery中,如何在继续循环之前等待模式在for循环中关闭? - In Javascript JQuery, how to wait for a modal to close in a for loop before continuing? 如何使函数等待回调,然后再继续 - How to make a function wait for call back, before continuing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM