异步运行PHP循环

时间:2020-05-08 06:13:37

标签: php asynchronous

我有一个需要在特定时间显示警报的应用程序,我需要在后台运行一些PHP。

echo "My content";

// Run below code asynchronously without interrupting the client's page from loading

sleep(60);
header("location: alert.php");

我希望它加载后在60秒后显示警报,而不是让客户端的浏览器等待所有PHP完成。出于安全原因,我也不能为此使用JavaScript。

1 个答案:

答案 0 :(得分:1)

您是在说“警报”,但是您是指Javascript alert()还是某种其他警报(如HTML块或HTML模式)?

无论如何,为什么不让您的PHP脚本这样做呢?

ob_end_flush(); // Disable output buffering
echo "My content. Please wait...";
do_some_stuff_that_takes_a_while();
echo "<script>alert('Done!');</script>";