jQuery AJAX导致页面加载缓慢 [英] Jquery AJAX causes SLOW page load

查看:216
本文介绍了jQuery AJAX导致页面加载缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的javascript函数:

I have a javascript function that looks like this:

function getAlerts() {
    $.post('getAlerts.php', function(data) {
        //Do stuff
        setTimeout(getAlerts, 1000);
    });
}

然后我进入document.ready块:

I then have in the document.ready block:

$(document).ready(function() {  
    setTimeout(getAlerts, 1000);
}

我希望每秒钟调用一次getAlerts函数,从页面加载后一秒钟开始.但是,当我将此回调添加到document.ready块时,这将导致整个页面的加载非常缓慢(如果有的话).我在这里想念什么?

I want the getAlerts function to be called every second, starting one second after the page has loaded. However, when I add this callback to the document.ready block, it causes the entire page to load extremely slowly, if at all. What am I missing here?

我还注意到,缓慢的页面加载仅在启用getAlerts函数并刷新页面两次之后才开始.前几次刷新似乎很好. AJAX调用是否可能在某处成为瓶颈?

I also notice that the slow page load only starts after enabling the getAlerts function, and refreshing the page a couple times. The first few refreshes seem fine. Is it possibly that the AJAX calls are bottlenecking somewhere?

推荐答案

我会尝试取出setTimeout,它将功能延迟一秒钟.另外,您可能想看看要发布到的php代码.如果它正在进行大量的mysql查询或其他可能会减慢您的加载时间的事情.

I would try taking out the setTimeout, it's delaying the function by a second. Also you may want to take a look at the php code that you are posting to. If it is making tons of mysql queries or something that could be slowing your load time as well.

这篇关于jQuery AJAX导致页面加载缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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