我正在使用Ajax加载带有Twitter Bootstrap选项卡的div中的另一个页面的内容,但是ajax加载页面的时间太长了。
没有Ajax的页面加载速度非常快!
ajax调用中加载的页面: 28.743376016617毫秒
不使用ajax加载页面: 0.00022506713867188毫秒
这是ajax调用的代码:
$(function() {
$("#MainTabs").tab();
$("#MainTabs").bind("show", function(e) {
var contentID = $(e.target).attr("data-target");
var contentURL = $(e.target).attr("href");
if (typeof(contentURL) != 'undefined')
$(contentID).html('<img src="<?php echo IMG_DIR; ?>loading/loading-large.gif" width="64" />').load(contentURL, function(){
$("#MainTabs").tab();
});
else
$(contentID).tab('show');
});
$('#MainTabs a:first').tab("show");
}); 这是一段PHP代码:
<?php
$start = microtime(TRUE); // Start counting
ob_start();
session_start();
$temp = microtime(TRUE) - $start;
echo $temp;
exit;
/*
* Here is the rest of the contents of the script, so I gave the 'exit' and even with the exit delay it that way!
*/有没有人知道发生了什么,以及如何帮助我?PHP代码非常简单,而且耗时太长!
谢谢!
发布于 2013-11-28 20:40:02
你的Ajax从后端加载html需要花费时间来生成html吗?
没有Ajax,你加载的数据更少,所以这就是它“快速运行”的原因。
如果您加载的数据不是那么常见,则通过异步脚本加载。在页面加载几秒钟后加载ajax div。
如果需要很长时间加载,
$(文档).ready( var xhr;var fn = function(){ if(xhr && xhr.readyState != 4){ xhr.abort();} xhr = $.ajax({ url:‘ajax/progress.ftl s.ftl’,成功:函数(数据){//做点什么} });};var interval = setInterval(fn,500);
);
发布于 2013-11-28 20:33:31
请使用.ajax()调用而不是.load()。
试试这个:
$(contentID).html('<img src="<?php echo IMG_DIR; ?>loading/loading-large.gif" width="64" />');
$(contentID).ajax(
url: contentURL,
type: "GET",//or POST
success: function(data){
$(contentID).html(data);
console.log(data);//it will show the error log if any [optional]
}
});发布于 2018-02-15 17:22:20
我也有同样的问题,在添加标题后:
header("Content-Length: xxx")它的运行速度非常快。
https://stackoverflow.com/questions/20266133
复制相似问题