AJAX(原型/ PHP)获得脚本执行过程中的部分状态更新原型、过程中、脚本、状态

2023-09-10 13:49:08 作者:一声兄弟大过天。

我有一个过程,我想在一个页面的后台运行。这一过程将需要一段时间的运行,可以说一个几分钟的时间。

I have a process I want to run in the background of a page. The process will take a while to run, lets say a few minutes.

我有它设置为使从该页面我可以点击一个按钮来启动进程关闭,稍等一下,然后它会完成,我可以有页面更新通知我,该过程已成功运行。我这样做原型和PHP。

I have it set up so that from the page I can click on a button to start the process off, wait a bit and then it will finish and I can have the page update notifying me that the process was run successfully. I'm doing this with prototype and php.

我想要做的是有状态更新,而进程正在运行。因此,它可以更新一个网页让我知道有多少记录已被处理为止或更新状态栏或类似的东西。

What I want to do is have status updates while the process is running. So it could update a the page letting me know how many records have been processed so far or update a status bar or something like that.

这可能吗?我只是不是一个巨大的JS大师,我可以做我已经描述的基本功能,但不知道是否有可能扩展它这样做一点额外的位,因为我还没有得到任何运气谷歌搜索或翻翻文档。

Is this possible? I'm just not a huge JS guru and I can do the basic functionality I've already described but was wondering if it was possible to extend it to do this little extra bit as I haven't had any luck googling or looking through the docs.

推荐答案

只需PHP,你会pretty的多少最终做投票。你需要生成你的长期运行的进程在后台运行,并且例如写文件的数据库,它的进步。

With just PHP you'll pretty much end up doing polling. You'll need to spawn your long-running process in the background, and for example write to a database of file what it's progress is.

浏览器就可以调用不同的PHP脚本每x秒,这回报告此情况。

The browser can then call a different php script every x seconds, which reports back with this status.

轮询虽然很烂,但是这就是你坚持,用PHP。

Polling sucks though, but that's what you're stuck with, with PHP.