I think your problem here is with the session.
When a script has an open session, it locks the session file. This means that any subsequent requests that use the same session identifier will be queued until the first script releases it in the session file. You can force this with session_write_close() - but that will not help you here, since you are trying to share progress information with the session file, so the post script will need to save the open and recorded session data.
You will need another way of exchanging data between post and progress scripts - if post contains session data that was opened during its execution, progress will never be able to access the session until post has completed execution. Perhaps you can use the session identifier to create a temporary file to which post has write access to which you place the progress indicator data. progress can check the file and return this data. There are many options for IPC (interprocess communication) - this is not particularly beautiful, but it has the advantage of maximum portability.
As a side note, do not pass setInterval() strings; pass functions. Therefore, your line should really read:
var progress = setInterval(ask, 500);
But - it would be better to use setTimeout() in the success / error handlers of the ajax ask() function. This is due to the fact that when using setInterval() new request will be initiated regardless of the state of the previous one. It would be more convenient to wait until the previous request completes before the next. So I would do something like this:
<script type="text/javascript"> // 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); } }); </script>
... although this still does not fix the session problem.
Daverandom
source share