jquery等待多个完整的事件 [英] jquery wait for multiple complete events

查看:145
本文介绍了jquery等待多个完整的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery的加载函数将一些内容加载到div中,我也想调用jQuery的 animate function。

I want to use jQuery's load function to load some content into a div, and I want to also call jQuery's animate function.

$('#div1').load('...', function() {
    // load complete
});

$('html,body').animate({
    ...: ...}, ..., '...', function(){
    // animate complete
});

我不想等待加载完成之前调用 animate 或反之亦然。

I don't want to wait for load to complete before calling animate or vice versa.

接下来我想调用第三个函数,但是直到加载 animate 之间的完整事件已被触发为止。

Following this I want to call a third function, but I don't want to call it until the complete event for both the load and the animate have been fired.

我该怎么做?

推荐答案

听起来像一份工作对于 Deferred http://api.jquery.com/category / deferred-object /

Sounds like a job for Deferred: http://api.jquery.com/category/deferred-object/

var load = $.Deferred(function (dfd) {
  $('#div1').load(…, dfd.resolve);
}).promise();

var animate = $('html,body').animate(…);

$.when(load, animate).then(function () {
  // Do your thing here!
});

这篇关于jquery等待多个完整的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆