Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中函数的异步执行_Javascript_Jquery_Function_Jquery Deferred - Fatal编程技术网

javascript中函数的异步执行

javascript中函数的异步执行,javascript,jquery,function,jquery-deferred,Javascript,Jquery,Function,Jquery Deferred,我想并行调用两个函数,分别是functiona()和functionb()。这些函数相互独立,可以说执行这两个函数所需的时间不是固定的。有时函数a()比函数b()花费更多的时间,反之亦然。但是还有另一个函数c(),它应该只在a()和b()两个函数都完成时执行 我应该如何使用jQuery的延迟对象来实现这一点?要实现这一点,可以让a()和b()函数返回延迟对象,在逻辑完成后,您可以解析这些对象。一旦前面两个函数都完成,就可以运行c()。试试这个: 函数a(){ var aDef=$.Deferr

我想并行调用两个函数,分别是function
a()
和function
b()
。这些函数相互独立,可以说执行这两个函数所需的时间不是固定的。有时函数
a()
比函数
b()花费更多的时间,反之亦然。但是还有另一个函数
c()
,它应该只在
a()
b()
两个函数都完成时执行


我应该如何使用jQuery的延迟对象来实现这一点?

要实现这一点,可以让
a()
b()
函数返回延迟对象,在逻辑完成后,您可以
解析这些对象。一旦前面两个函数都完成,就可以运行
c()
。试试这个:

函数a(){
var aDef=$.Deferred();
setTimeout(函数(){
aDef.决议(“已完成”);
}, 1000);
返回aDef;
}
函数b(){
var bDef=$.Deferred();
setTimeout(函数(){
b.决议(“b完成”);
}, 3000);
返回bDef;
}
函数c(){
console.log('all done!')
}
log('running…');
$.when(a(),b()).done(函数(a,b){
控制台日志(a);
控制台日志(b);
c();
})
您可以使用jQuery.when()执行此操作。请在以下位置阅读有关此的文档:


我将使用一个全局变量来确定操作状态,并每100毫秒(或者如果需要,每毫秒)执行一次轮询

var myStatus={
“a”:错,
“b”:错
};
函数a(){
myStatus[“a”]=真;
log(myStatus['a']);
}
函数b(){
myStatus[“b”]=true;
}
函数getStatusText(){
var s='不完整';
if(myStatus.a&&myStatus.b){
s=‘全部完成’;
}否则{
if(myStatus.a){
s=‘完整的’;
}
如果(myStatus.b){
s=‘b完成’;
}
}
返回s;
}
函数c(){
//检查运行状态
var statusText=getStatusText();
document.getElementById('status')。innerHTML=statusText;
}
设定间隔(
函数(){
c()
}, 100);
设置a()完成设置b()完成

操作状态

请参考Jquery延迟和承诺方法来处理呼叫


这并不是对这个问题的确切回答。我不使用延迟或类似的方式

但我想展示我经常做的事情:添加onReady回调,作为a()和b()的参数。我将这些回调添加到任何需要时间执行的自写函数中

function a(onready) {
  // let's say we get Ajax data
  $.ajax({
    url: 'data.php',
    success: function(data) {
      $('#message').html(data);
      if(typeof onready == 'function') {
        onready();   // you might also want to add message as a parameter, like onready(data), or anready('Data okay'), ...
      }
    }
  });
}

function b(onready) {
  // let's say we sort <table> rows
  sortTable('my_table', 'my_row', 'ASC');  // this function (not provided here) is not asynchronous, it just takes time before it's done
  if(typeof onready == 'function') {
    onready();   
  }
}

function c() {
  alert('Yippy!');
}

$(document).ready(function() {  // or this could be after the client clicks on a button, or so
  var aReady = false;
  var bReady = false;

  a(function() {
    aReady = true;
    if(aReady && bReady) {
      c();
    }
  });

  b(function() {
    bReady = true;
    if(aReady && bReady) {
      c();
    }
  });

}); 
功能a(onready){
//假设我们得到了Ajax数据
$.ajax({
url:'data.php',
成功:功能(数据){
$('#message').html(数据);
if(typeof onready=='function'){
onready();//您可能还希望将消息作为参数添加,如onready(data)或anready('data ok')。。。
}
}
});
}
功能b(onready){
//假设我们对行进行排序
sortTable('my_table'、'my_row'、'ASC');//此函数(此处未提供)不是异步的,它只是需要时间才能完成
if(typeof onready=='function'){
onready();
}
}
函数c(){
警惕(‘耶!’);
}
$(document).ready(function(){//),或者这可能是在客户端单击按钮之后,或者是在其他情况下
var aReady=假;
var bReady=false;
a(函数(){
aReady=true;
如果(区域和区域){
c();
}
});
b(职能({
bReady=true;
如果(区域和区域){
c();
}
});
}); 

Check it Out无法与193k用户竞争。。。我回答得太慢了:(
function a(onready) {
  // let's say we get Ajax data
  $.ajax({
    url: 'data.php',
    success: function(data) {
      $('#message').html(data);
      if(typeof onready == 'function') {
        onready();   // you might also want to add message as a parameter, like onready(data), or anready('Data okay'), ...
      }
    }
  });
}

function b(onready) {
  // let's say we sort <table> rows
  sortTable('my_table', 'my_row', 'ASC');  // this function (not provided here) is not asynchronous, it just takes time before it's done
  if(typeof onready == 'function') {
    onready();   
  }
}

function c() {
  alert('Yippy!');
}

$(document).ready(function() {  // or this could be after the client clicks on a button, or so
  var aReady = false;
  var bReady = false;

  a(function() {
    aReady = true;
    if(aReady && bReady) {
      c();
    }
  });

  b(function() {
    bReady = true;
    if(aReady && bReady) {
      c();
    }
  });

});