在javascript运行时离开页面

时间:2014-06-25 20:52:13

标签: javascript php jquery

我遇到了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();

编辑:删除了在后台运行的Steam WEB API,它现在运行良好。

2 个答案:

答案 0 :(得分:1)

听起来很像 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.

?>

我实际上在我的长期民意调查中做了这种顽皮的事情:

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

}

答案 1 :(得分:0)

已删除Steam WEB API在后台运行,现在运行正常。