如何在为最终用户加载页面之前等待完成而不执行PHP脚本?

时间:2022-08-24 17:36:57

I am working on a site plugin that takes advantage of the YouTube API to grab some data for a specific channel. The problem is that the code takes several seconds to load on larger channels meaning the user has to wait quite a bit of time for the script to finish loading before they can view each page. Most of the time the data retrieved isn't even relevant to them but the script still needs to run its checks. So how can I initialize the PHP script without the user having to wait for it?

我正在开发一个网站插件,利用YouTube API抓取特定频道的某些数据。问题是代码需要几秒钟来加载更大的通道,这意味着用户必须等待相当多的时间让脚本完成加载才能查看每个页面。大多数情况下,检索到的数据甚至与它们无关,但脚本仍需要运行其检查。那么如何在不让用户等待的情况下初始化PHP脚本呢?

My first thought is to use AJAX and make a request to the remote script but will that cause parts of the page to hang as the script is running? I don't need the results from the script, I just need the script to run and do its own thing.

我的第一个想法是使用AJAX并向远程脚本发出请求,但这会导致页面的某些部分在脚本运行时挂起吗?我不需要脚本的结果,我只需要脚本运行并做自己的事情。

2 个解决方案

#1


0  

You can use exec to start a separate PHP script. And adding & at the end will make exec return imidiately.

您可以使用exec启动单独的PHP脚本。并且最后添加&将使exec返回imidiately。

Let script.php do the heavy stuff and call it from a mall script like so:

让script.php完成繁重的工作并从商城脚本中调用它,如下所示:

exec("php script.php > /dev/null &");

You need to have a *nix server environment. Don't think it will work on Windows platform.

您需要有一个* nix服务器环境。不要认为它适用于Windows平台。

#2


0  

AJAX stands for Asynchronous JavaScript and XML, which means that, when using it, the execution of the rest of the code will not be blocked.

AJAX代表异步JavaScript和XML,这意味着,在使用它时,不会阻止其余代码的执行。

You can make an AJAX request to the script you want to execute and then keep rendering the page normally. The AJAX request will be performed in the background, and won't be of any disturb whatsoever.

您可以对要执行的脚本发出AJAX请求,然后继续正常呈现页面。 AJAX请求将在后台执行,并且不会受到任何干扰。

#1


0  

You can use exec to start a separate PHP script. And adding & at the end will make exec return imidiately.

您可以使用exec启动单独的PHP脚本。并且最后添加&将使exec返回imidiately。

Let script.php do the heavy stuff and call it from a mall script like so:

让script.php完成繁重的工作并从商城脚本中调用它,如下所示:

exec("php script.php > /dev/null &");

You need to have a *nix server environment. Don't think it will work on Windows platform.

您需要有一个* nix服务器环境。不要认为它适用于Windows平台。

#2


0  

AJAX stands for Asynchronous JavaScript and XML, which means that, when using it, the execution of the rest of the code will not be blocked.

AJAX代表异步JavaScript和XML,这意味着,在使用它时,不会阻止其余代码的执行。

You can make an AJAX request to the script you want to execute and then keep rendering the page normally. The AJAX request will be performed in the background, and won't be of any disturb whatsoever.

您可以对要执行的脚本发出AJAX请求,然后继续正常呈现页面。 AJAX请求将在后台执行,并且不会受到任何干扰。