gpt4 book ai didi

javascript - 使用 PHP 的 Ajax 调用非常慢

转载 作者:可可西里 更新时间:2023-10-31 23:31:30 28 4
gpt4 key购买 nike

我正在使用 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代码非常简单,耗时太长!

谢谢!

最佳答案

您的 Ajax 是否从后端加载 html 需要时间来生成 html?

在没有 Ajax 的情况下,您加载的数据较少,因此它的运行速度很快。

如果您加载不常见的数据,则通过异步脚本加载。在页面加载后几秒钟后加载 ajax div。

  1. 如果加载时间较长,请取消 ajax 请求。

    $(document).ready(
    var xhr;

    var fn = function(){
    if(xhr && xhr.readyState != 4){
    xhr.abort();
    }
    xhr = $.ajax({
    url: 'ajax/progress.ftl',
    success: function(data) {
    //do something
    }
    });
    };

    var interval = setInterval(fn, 500);

    );

关于javascript - 使用 PHP 的 Ajax 调用非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20266133/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com