Progress bar with PHP & Ajax

后端 未结 2 563
名媛妹妹
名媛妹妹 2020-12-15 13:16

I am working on progress bar which updates progress using ajax requests and session variables. When my program performs time consuming operation such as sending many emails

2条回答
  •  执笔经年
    2020-12-15 13:59

    I think your problem here is session related.

    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 session_write_close() - but this won't really help you here, as you are trying to share the progress info with the session file so post script would need to keep the session data open and writable.

    You will need to come up with another way of sharing data between the post and progress scripts - if post has the session data open throughout it's execution, progress will never be able to access the session data until after post has finished executing. Maybe you could use the session ID to create a temporary file which post has write access to, in which you put the progress indicator data. The progress 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.

    As a side note - please don't pass strings to setInterval(), pass functions. So your line should actually read:

    var progress = setInterval(ask, 500);
    

    But - it would be better to use setTimeout() in the success/error handlers of the ask() ajax function. This is because by using setInterval(), 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:

    
    

    ...although this still doesn't fix the session problem.

提交回复
热议问题