WalkerJetBat
WalkerJetBat

Reputation: 75

Leaving page while javascript is running

I'm having an issue with javascript + php. I'm running background javascript function (Steam Web API) to run a .php file at background. However, sometimes it takes over 10 seconds to load. Whenever I change the page while the background function is still working, the new page loads slower, like the progress bar is hanging. Here is the code:

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

EDIT: Removed Steam WEB API from running at background and it works perfect now.

Upvotes: 1

Views: 73

Answers (2)

WalkerJetBat
WalkerJetBat

Reputation: 75

Removed Steam WEB API from running at background and it works perfect now.

Upvotes: 0

svidgen
svidgen

Reputation: 14302

Sounds like KevinB nailed it to me. Your long-running request is holding onto a session file. New requests will stall until the file is released. I've run into this a good number of times.

The solution: On the PHP side, read the session info you need and immediately close the file or DB connection:

<?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.

?>

I actually do this naughty sort of stuff in my long polls:

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

}

Upvotes: 1

Related Questions