Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think your problem here is session related.</p> <p>When a script has an open session, it has a lock on the session file. This means that any subsequent requests which use the same session ID will be queued until the first script has released it's lock on the session file. You can force this with <a href="http://php.net/manual/en/function.session-write-close.php"><code>session_write_close()</code></a> - but this won't really help you here, as you are trying to share the progress info with the session file so <code>post</code> script would need to keep the session data open and writable.</p> <p>You will need to come up with another way of sharing data between the <code>post</code> and <code>progress</code> scripts - if <code>post</code> has the session data open throughout it's execution, <code>progress</code> will never be able to access the session data until after <code>post</code> has finished executing. Maybe you could use the session ID to create a temporary file which <code>post</code> has write access to, in which you put the progress indicator data. The <code>progress</code> can check the file and return that data. There are many options for IPC (inter-process communication) - this is not a particularly beautiful one but it does have the advantage of maximum portability.</p> <p>As a side note - please don't pass strings to <code>setInterval()</code>, pass functions. So your line should actually read:</p> <pre><code>var progress = setInterval(ask, 500); </code></pre> <p>But - it would be better to use <code>setTimeout()</code> in the <code>success</code>/<code>error</code> handlers of the <code>ask()</code> ajax function. This is because by using <code>setInterval()</code>, a new request will be initiated regardless of the state of the previous one. It would be more efficient to wait until the previous request has finished before initiating the next one. So I would do something more like this:</p> <pre><code>&lt;script type="text/javascript"&gt; // We'll set this to true when the initail POST request is complete, so we // can easily know when to stop polling the server for progress updates var postComplete = false; var ask = function() { var time = new Date().getTime(); $.ajax({ type: 'get', url: '/url/to/progress' + '?time=' + time, success: function(data) { $("#progress").html(data); if (!postComplete) setTimeout(ask, 500); } }, error: function() { // We need an error handler as well, to ensure another attempt gets scheduled if (!postComplete) setTimeout(ask, 500); } } }); } $("#test").click(function() { // Since you only ever call post() once, you don't need a seperate function. // You can just put all the post() code here. var time = new Date().getTime(); $.ajax({ type: 'post', url: '/url/to/post' + '?time=' + time, data: { "some": "data" }, success: function(data) { postComplete = true; alert(data); } error: function() { postComplete = true; } }); if (!postComplete) setTimeout(ask, 500); } }); &lt;/script&gt; </code></pre> <p>...although this still doesn't fix the session problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload