douzi8112 2016-01-08 06:09
浏览 43
已采纳

PHP顺序执行ajax调用

This is driving me nuts for a while now. I have an ajax call like so:

function update()
{
    $.get("update.php", function(data)
    {
        $("#output-progress").html(data);
    });
}

And I call it like so:

window.setInterval(function()
    {
        update();
    }, 2000);
}

Then I have another calc function which is also called:

function calc()
{
    $.get("calc.php", function(data)
    {
        //whole bunch of lines to re-render page
    });
}

So the idea is that while calc() is running, the update() will periodically update a div on the progress.

However what is happening is that if I open the console and check the calls to update() is triggered every 5 seconds, but they just stall and they complete only after the calc() has returned. I first thought this was a browser/jQuery issue, but if I log both the functions into separate log files in PHP then the update() gets logged only after the calc() finishes!

Im not sure what is going on here, any pointers are greatly appreciated!

  • 写回答

1条回答 默认 最新

  • douxiji8707 2016-01-08 07:14
    关注

    Most likely, you are using sessions, and both calc.php and update.php access session data. In order to ensure data consistency in sessions, access to session data is locked, so only one php process can access the session at a time. This means that while calc.php has the session, no other page access can read it.

    What you will want to do is call session_write_close() after calc.php has finished anything that might require access to the session, and before it starts its time-consuming task.

    session_write_close() writes the current session's data and releases the lock. Once calc.php is no longer holding to the session, accesses from update.php can read it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding