2012-07-01 67 views
1

我想写一个程序,在2个对象因ajax函数的回调而返回后执行某些操作。jquery从ajax调用的回调推迟

我知道使用jQuery的经典例子,当():

$.when($.get("http://localhost:3000/url1"), 
$.get("http://localhost:3000/url2").done(//do something)); 

但在我的情况,我不想触发时的AJAX功能的执行,我想的时候从执行ajax函数的回调中触发。例如:

$.get("http://localhost:3000/url1", function(data){ 
    function(){ 
    //do something with the data, and return myobj1; 
    } 
}); 

$.get("http://localhost:3000/url2", function(data){ 
    function(){ 
    //do something with the data, and return myobj2; 
    } 
}); 

$.when(obj1, obj2).done(function(){ 
    //do something with these 2 objects 
}); 

但当然,这是行不通的。想法?

回答

5

这实际上应该工作。 jQuery.when()需要多个参数和闪光一次,他们都完成了返回每个结果的参数作为数组:

var req1 = $.get("http://localhost:3000/url1"); 
var req2 = $.get("http://localhost:3000/url2"); 

$.when(req1, req2).done(function(res1, res2) { 
    //do something with these 2 objects 
}); 

如果你不想来处理请求一起,你可以创建自己的deferreds并使用这些:

var deferred1 = $.Deferred(), 
    deferred2 = $.Deferred(); 

$.get("http://localhost:3000/url1", function(data){ 
    function(){ 
     //do something with the data, and return myobj1; 
     deferred1.resolve(myobj1); 
    } 
}); 

$.get("http://localhost:3000/url2", function(data){ 
    function(){ 
     //do something with the data, and return myobj2; 
     deferred2.resolve(myobj2); 
    } 
}); 

$.when(deferred1, deferred2).done(function(){ 
    //do something with these 2 objects 
}); 
+0

是的,你发布的作品。但问题在于,当$ .get函数返回时启动了when,但如果您注意到上述情况,我会为每个$ .get请求发生回调,并且何时不会等待这些回调进行处理,只是当$ .get函数完成时。所以,何时发射太早。 –

+0

@RyanOgle如果你不能因为某种原因一起处理这些响应,你可以创建你自己的延迟并使用它们。通过示例更新答案。 – Trevor

+0

@ Trevor我必须在这里失去一些东西。在你添加的例子中,when()不应该触发,直到2延迟得到解决?我没有得到这个工作。看看这个简单的例子:http://jsfiddle.net/ax4u4/当()被触发时,即使延迟对象永远不会被解析。 –

0

,或者你可以做你自己控制

 $(function(){$('body').addClass('doc-ready')}) 
    var thingsToLoad = ['blabla.js','blublu.js','weee.js']; 

    var launch = function(){ 

    // do whatever you want to do after loading is complete 
    // this will be invoked after dom ready. 
    // objectCollection will have everything you loaded. 

    // and you can wrap your js files in functions, and execute whenever you want. 

    } 

    var loadTester = (function() { 
     var loadCounter = 0, 
      loadEnds  = thingToLoad.length; // total number of items needs to be loaded 
     return function() { 
      loadCounter += 1; 
      if (loadCounter === loadEnds) { 
      if ($('body').hasClass('doc-ready')) { 
       launch(); 
      } else { 
       /* if body doesnt have ready class name, attach ready event and launch application */ 
       $(function() { 
       launch(); 
       }); 
      } 
      } 
     } 
     }()); 


$.each(thingsToLoad, function(i) { 
    $.ajax({ 
     url  : thingsToLoad[i], 
     mimeType : 'application/javascript', 
     dataType : 'script', 
     success : function(data) { 
     loadTester(); 
     } 
    }); 
    }); 

添加文件到thingsToLoad阵列, 在最后它将被迭代并在成功后加载,它会初始化为 loadTester

loadTester将检查您的thingsToLoad阵列的长度,当加载的文件与文件长度匹配且dom处于就绪状态时,它将为launch()

如果你只是加载HTML文件或文本文件,可以把它们(data在AJAX功能)为loadTester和积聚在那里(就像那些loadCounterloadEnds私人变种内),并通过积累数组或对象到launch()