最好的DOM的时间来运行的jQuery AJAX请求? [英] Best DOM time to run jQuery AJAX request?

查看:162
本文介绍了最好的DOM的时间来运行的jQuery AJAX请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个字preSS AJAX的插件,拉像数从各种社交媒体网站的用户。该插件使用一个jQuery的AJAX请求最初发出请求到所有的社会媒体网站。

所有不同的社交媒体网站的初始呼叫可以说是相当缓慢的。最初的电话(S)后,像计数缓存在Word中preSS数据库。

我建立一个自定义的jQuery的功能,使AJAX请求,并想知道什么是扳平AJAX功能的最佳活动。

。就绪

  $(文件)。就绪(函数(){
    // code在这里
});
 

.load

  $(窗口).load(函数(){
    // code在这里
});
 

解决方案

下面是加载并准备之间的差额:
准备:当HTML文件被加载和DOM准备
执行 载:执行完成后页面完全加载,包括所有的帧,对象

更新:
在由@rnevius评论说,你可以运行,没有任何DOM事件的要求,这样你就可以执行请求的脚本加载。

所以,最早的是尽可能做到这一点的就绪

I am building a WordPress AJAX plugin that pulls the Like count for a user from various Social media sites. The plugin uses one jQuery AJAX request to initially make the request to all the Social Media sites.

The initial call to all the different Social Media sites can be quite slow. After the initial call(s) the Like counts are cached in the WordPress database.

I am building a custom jQuery function to make the AJAX request and am wondering what is the best event to tie the AJAX function to.

.ready

$(document).ready(function() {
    // code here
});

or .load

$(window).load(function() {
    // code here
});

解决方案

Here is the deference between load and ready:
ready: executes when HTML-Document is loaded and DOM is ready
load: executes when complete page is fully loaded, including all frames, objects

Update:
As said in the comments by @rnevius you can run the request without any DOM event, so you can execute the request as the script loads.

So the earliest as possible is to do this on ready

这篇关于最好的DOM的时间来运行的jQuery AJAX请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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