jQuery的。当和多个.load [英] Jquery .when and multiple .load
本文介绍了jQuery的。当和多个.load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想有一个回调函数的动作完成后,我想是这样的:
I want to have one callback function after actions are done, I'm trying something like this:
$.when(
$('#detail1').load('/getInfo.php'),
$('#detail2').load('/getOther.php')
).then(function(a,b){
alert("done");
});
的问题是,在操作完成之前的回调函数烧制
The problem is that the callback function is firing before the actions are finished.
推荐答案
这是因为jQuery.when()预计jQuery.Deferred实例,而负载()返回一个jQuery的实例(见的 http://api.jquery.com/jQuery.when/ 和 HTTP ://api.jquery.com/load/ )
This is because jQuery.when() expects jQuery.Deferred instances while load() returns an jQuery instance (see http://api.jquery.com/jQuery.when/ and http://api.jquery.com/load/).
您可以解决此问题:
// Create two Deferred instances that can be handed to $.when()
var d1 = new $.Deferred();
var d2 = new $.Deferred();
// Set up the chain of events...
$.when(d1, d2).then(function() {
alert('done');
});
// And finally: Make the actual ajax calls:
$('#detail1').load('/getInfo.php', function() { d1.resolve(); });
$('#detail2').load('/getOther.php', function() { d2.resolve(); });
这篇关于jQuery的。当和多个.load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文