简体   繁体   English

Ajax放慢页面

[英]ajax slowing down page

I have a webpage with ajax. 我有一个关于ajax的网页。 Ajax HAS to load some data from other website, which takes some time. Ajax HAS需要从其他网站加载一些数据,这需要一些时间。 During that "load" time, my site is not responding well to user input (going to some other page etc). 在该“加载”时间内,我的网站对用户输入的响应不佳(转到其他页面等)。 Before anyone yells - yes, I know, it's because of ajax. 在有人大喊之前-是的,我知道,这是因为ajax。 :) :)

My question is - is there any way to limit ajax bandwidth? 我的问题是-有没有办法限制Ajax带宽? Fast website is my priority, loading data in the background is secondary. 快速网站是我的首要任务,在后台加载数据是次要的。

So, if i have a javascript which calls php (ajax), and then php using cUrl gets some data and returns it back to javascript (actual code is to long to post it here, no one would read it for sure :D) is there any way to prevent that mechanism from blocking my page until it's done? 因此,如果我有一个调用php(ajax)的javascript,然后使用cUrl的php获取了一些数据,并将其返回给javascript(实际代码很长就可以在此处发布,没有人可以肯定地阅读它:D)是有什么方法可以阻止该机制在完成之前阻止我的页面?

The question is what is slowing your page down? 问题是什么使您的页面速度变慢? It could be several things from yoyur description. 从您的描述中可能有几件事。

A couple of things to bear in mind with ajax: Ajax需要牢记的几件事:

1: most browsers will only do two xhr requests per domain at the same time 1:大多数浏览器每个域只能同时执行两个xhr请求

2: if the result of the call xhr needs to be parsed and the parsing is heavy, like eval'ing json in old browsers, it can have a freezing effect on the browser while the data parsing occurs (js is single threaded by nature, unless you count webworkers in html5) 2:如果需要解析调用xhr的结果并且分析很繁琐(例如在旧的浏览器中评估json),那么在进行数据解析时(它本质上是单线程的,除非您在html5中计算网络工作者)

3: if the result of the ajax forces the browser to re-render the page due to dom injection, the rendering can feel slow and freeze the browser a bit in old browsers. 3:如果ajax的结果由于dom注入而迫使浏览器重新呈现页面,则呈现速度可能会变慢并使浏览器在旧的浏览器中冻结。

4: if you have bandwidth constraints, try to run your xhrs when the browser is done with other stuff, for instance, wait for domReady on pageload 4:如果您有带宽限制,请在浏览器处理完其他内容后尝试运行xhrs,例如,在页面加载时等待domReady

Look for asynchronous loading of Javascript source files, I hope already you are using asynchronous XHR calls. 寻找Java语言源文件的异步加载,希望您已经在使用异步XHR调用。 See the Google Analytics tracking documentation for example usage: 有关示例用法,请参见Google Analytics(分析)跟踪文档:

<script type="text/javascript">  (function() {
    var ga = document.createElement('script');     
    ga.type = 'text/javascript';
    ga.async = true;
    ga.src = ('https:'   == document.location.protocol ? 'https://ssl'   
            : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(ga, s);
    })();
   </script>

http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM