我可以得到一个jQuery递延上的document.ready()? [英] Can I get a jQuery Deferred on document.ready()?

查看:100
本文介绍了我可以得到一个jQuery递延上的document.ready()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本加载我在做一个Ajax请求得到一些翻译权。这应该文档后总是返回准备好,因为我在页面底部装载我的剧本,但我还是好奇,如果有可能获得的延迟的对象

这样,它有可能确保两个,该文件已经准备好与Ajax调用做其他事情,比如之前成功返回像这样的:

  $。当($阿贾克斯('翻译')的document.ready())
。然后(函数(){
    //开始在这里做的东西
});
 

解决方案

您可以将延迟的对象与文档使用数据()关联< /一>,并决心()它在你的就绪处理程序。这样一来,你应该能够使用存储的延迟对象 $。当()

  $(文件)的.data(readyDeferred,$ .Deferred())。就绪(函数(){
    $(文件)的.data(readyDeferred)解决()。
});

$。当($。阿贾克斯(转换),$(文件)的.data(readyDeferred))
 。然后(函数(){
    //开始在这里做的东西。
});
 

Right after my script is loaded I am making an Ajax request to get some translations. This should always return after the document is ready since I am loading my scripts at the bottom of the page, but I am still curious if it would be possible to get a Deferred Object on the document ready state.

That way it would be possible to make sure that both, the document is ready and the Ajax call returned successfully before doing anything else, e.g. like this:

$.when( $.ajax('translations'), document.ready())
.then(function(){
    // Start doing stuff here
});

解决方案

You can associate a deferred object with the document using data(), and resolve() it in your ready handler. This way, you should be able to use the stored deferred object with $.when():

$(document).data("readyDeferred", $.Deferred()).ready(function() {
    $(document).data("readyDeferred").resolve();
});

$.when($.ajax("translations"), $(document).data("readyDeferred"))
 .then(function() {
    // Start doing stuff here.
});

这篇关于我可以得到一个jQuery递延上的document.ready()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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