加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 一个ajax调用阻止其他ajax调用

发布时间:2020-12-13 17:26:15 所属栏目:PHP教程 来源:网络整理
导读:加载页面后,我会对php脚本执行Ajax调用,这会更新我的服务器.但是这个脚本有时可能需要一分钟才能完成,而且在脚本运行时,我无法执行其他需要处理的Ajax调用 – 即第一个Ajax调用不应该中断其他Ajax调用.知道怎么做吗? 第一个Ajax调用: $(document).ready(fu
加载页面后,我会对php脚本执行Ajax调用,这会更新我的服务器.但是这个脚本有时可能需要一分钟才能完成,而且在脚本运行时,我无法执行其他需要处理的Ajax调用 – 即第一个Ajax调用不应该中断其他Ajax调用.知道怎么做吗?

第一个Ajax调用:

$(document).ready(function () { 
    $.ajax({
        url: "checkForUpdatesByCoach.php",success: function(arg){
            if(arg == "200"){
                $('body').prepend("<div style='text-align:center;margin-bottom:-12px;' onClick='location.reload()' class='alert alert-success'>Dine hold er blevet opdateret.Tryk for at opdatere!</div>").fadeIn("slow");  
            }
        }
});
});

第二次Ajax调用(用户触发的调用):

$.ajax({
    type: "POST",data: {data:dataAjax},url: "updateSwimmer1.php",success: function(arg){
            //updating UI
        }
    });

解决方法

adeno上面的评论是正确的.

“in PHP only one script at a time can operate on the same session,so
as to not overwrite session data etc. So when doing two ajax calls to
PHP scripts within the same session,the second has to wait for the
first to finish”

为了帮助加快速度,您可以提前写入和结束会话(session_write_close())以释放会话锁定并允许使用会话的另一个脚本继续.

注意:在调用session_write_close之后,您仍然可以从$_SESSION变量中读取,但是您可能不再写入它.

你可以在这里找到一个很好的例子:PHP Session Locks – How to Prevent Blocking Requests

从上面的链接提供的示例:

<?php
// start the session
session_start();

// I can read/write to session
$_SESSION['latestRequestTime'] = time();

// close the session
session_write_close();

// now do my long-running code.

// still able to read from session,but not write
$twitterId = $_SESSION['twitterId'];

// dang Twitter can be slow,good thing my other Ajax calls
// aren't waiting for this to complete
$twitterFeed = fetchTwitterFeed($twitterId);

echo json_encode($twitterFeed);
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读