在javascript运行时离开页面

在javascript运行时离开页面,javascript,php,jquery,Javascript,Php,Jquery,我对javascript+php有一个问题。我正在运行后台javascript函数(Steam Web API),以便在后台运行.php文件。但是,有时加载需要10秒以上。每当我在后台功能仍在工作时更改页面时,新页面的加载速度就会变慢,就像进度条挂起一样。代码如下: function loaddata() { document.getElementById('page_loading').style.display = "block"; $.post("<? echo $j

我对javascript+php有一个问题。我正在运行后台javascript函数(Steam Web API),以便在后台运行.php文件。但是,有时加载需要10秒以上。每当我在后台功能仍在工作时更改页面时,新页面的加载速度就会变慢,就像进度条挂起一样。代码如下:

function loaddata()
{
    document.getElementById('page_loading').style.display = "block";
    $.post("<? echo $js_url; ?>",{
        refreshall: "true"
    },
    function(data)
    {
        document.getElementById('page_loading').style.display = "none";
        <? if ($curpage == "My Account") { echo '
            document.getElementById("profileloadingBlanket").style.display = "none";
            document.getElementById("profileloadingDiv").style.display = "none";
            $("#profile_Area").load("account.php #profile_Area").fadeIn();
    '; } ?>
    });
}
loaddata();
函数loaddata()
{
document.getElementById('page_loading').style.display=“block”;
$邮政编码(“{
刷新所有:“真实”
},
功能(数据)
{
document.getElementById(“页面加载”).style.display=“无”;

听起来像是KevinB给了我一个明确的答案。你的长时间运行的请求保留在一个会话文件上。新的请求将暂停,直到文件被释放。我已经遇到过很多次了

解决方案:在PHP端,读取所需的会话信息并立即关闭文件或数据库连接:

<?php

  // populate $_SESSION
  session_start();

  // if you're using default session management OR any sort of locking,
  // close the session file
  session_write_close();

  //
  // do lots of stuff here
  //

  // if you need to write out to the session, I'm pretty sure you can
  // re-attach like this, which I think re-creates $_SESSION.
  session_start();

  // so, anything you want to persist must be changed after re-attaching.
  $_SESSION['some'] = 'value';

  // fin.

?>

删除了后台运行的Steam WEB API,现在它工作得很好。

这可能是因为您在进程上使用的会话需要10秒。编辑:我知道为什么加载需要10秒。事实并非如此。情况是:在页面仍在加载数据时更改页面会减慢页面的加载速度。我对php了解不多除此之外,使用会话可以防止使用同一会话的多个请求一次完成多个请求。可能有一种方法可以修复或避免它,我只是不知道。@WalkerJetBat这很奇怪。你能更新你的问题并发布答案来显示修复吗?好的。我使用的是Steam Web API,这就是导致问题的原因问题。我用cron job做的。
while (!$finished) {

  // at-sign suppresses errors if we're already attached
  @session_start();
  session_write_close();

  // do some work
  $finished = $finished || examine_session_for_message();
  $finished = $finished || are_we_over_30_seconds_yet();
  $finished = $finished || check_other_places_for_message();
  $finished = $finished || are_we_done_computing_stuff();

  if (!$finished) {
    usleep(250000);
  }

}