一个Ajax调用块等Ajax调用 [英] One ajax call block other ajax call

查看:104
本文介绍了一个Ajax调用块等Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载我的网页,我执行一个Ajax调用PHP脚本,用于更新我的服务器。但是这个脚本有时需要一分钟时间才能完成,并且脚本运行时,我无法执行其他Ajax调用,这是我需要处理 - 即第一个Ajax调用不应打断对方Ajax调用。任何想法如何做到这一点?

首先调用Ajax:

  $(文件)。就绪(函数(){
    $阿贾克斯({
        网址:checkForUpdatesByCoach.php
        成功:函数(ARG){
            如果(ARG ==200){
                $(身体)prePEND(<股利风格=文本对齐:中心;保证金底:-12px;'的onClick ='location.reload()'类='警戒警报,成功 >用餐持有ER blevet opdateret.Tryk在opdatere<!/ DIV>中)淡入(慢);
            }
        }
});
});
 

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

  $。阿贾克斯({
    键入:POST,
        数据:{数据:dataAjax},
        网址:updateSwimmer1.php
        成功:函数(ARG){
            //更新用户界面
        }
    });
 

解决方案

腺的上述评论是正确的。

  

在PHP在同一时间只有一个脚本可以在同一次会议上进行操作,所以   为不覆盖会话数据等,所以做两AJAX调用时   在同一会话内PHP脚本,第二个有等待   首先要完成

要帮助加快东西可以写入和早期结束会话(session_write_close())以释放会话锁和使用会议上继续允许其他脚本。

请注意:你仍然可以从您的$ _SESSION变量读取调用session_write_close后,但你可能不再写它

你可以找到一个很好的例子在这里: PHP会话锁 - 如何prevent阻止请求

例如,从上面的链接提供的:

 < PHP
//启动会话
在session_start();

//我可以读/写会话
$ _SESSION ['latestRequestTime'] =时间();

//关闭会话
session_write_close();

//现在做我的长期运行code。

//仍然能够从会话读取,但不能写入
$ twitterId = $ _SESSION ['twitterId'];

//党的Twitter可能会很慢,还好我的其他Ajax调用
//不等待这个完成
$通过twitterfeed = fetchTwitterFeed($ twitterId);

回声json_en code($通过twitterfeed);
?>
 

Once my page is loaded, I perform an Ajax call to a php script, which updates my server. However this script can sometimes take over a minute to complete, and while the script is running, I am unable to perform other Ajax calls, which I need to handle - i.e the first Ajax call should not interrupt the other Ajax calls. Any idea how to do this?

First Ajax call:

$(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");  
            }
        }
});
});

Second Ajax call (a user triggered call):

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

解决方案

adeno's comment above is correct.

"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"

to help speed things up you can write to and end a session early(session_write_close()) to release the session-lock and allow another script using the session to continue.

note: you can still read from your $_SESSION variable after calling session_write_close but you may no longer write to it.

you can find a good example of this here: PHP Session Locks – How to Prevent Blocking Requests

example provided from the link above:

<?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);
?>

这篇关于一个Ajax调用块等Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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