javascript - JavaScript 运行时离开页面

标签 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,现在可以完美运行。

最佳答案

听起来KevinB把它钉在了我身上。您的长时间运行的请求正在保存 session 文件。新请求将停止,直到文件被释放。我已经遇到过很多次了。

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

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

}

关于javascript - JavaScript 运行时离开页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24418342/

相关文章:

javascript - 为什么 onClick 对我的按钮不起作用?

php - 我似乎无法弄清楚为什么我的图像上传脚本不起作用

php - 存储服务器端数据的最佳方式?

javascript - 对于 jQuery "media query"类型的情况使用 setTimeout() 是否有任何不足

javascript - 将元素推到父级的底部

javascript - 将参数传递给 Web 组件以在类中使用失败

javascript - Vue : get list of components

javascript - 是否可以在定义对象构造函数值后更改它们?

php - 使用比数据库列更多的键从关联数组中混合 Eloquent 模型对象

Javascript 获取元素的文本