Php,在执行一个动作前等待 5 秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6730855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php, wait 5 seconds before executing an action
提问by user849690
I have a .php script that I use for creating the list of my products. I am on shared hosting, so I can't do a lot of queries otherwise I get a blank page.
我有一个 .php 脚本,用于创建我的产品列表。我在共享主机上,所以我不能做很多查询,否则我会得到一个空白页面。
This is how I use my script now:
这就是我现在使用脚本的方式:
script.php?start=0&end=500&indexOfFile=0 ->> make a product0.txt file with first 500 products
script.php?start=501&end=1000&indexOfFile=1 ->> product1.txt file with another 500 products
script.php?start=1001&end=1500&indexOfFile=2 ->> product2.txt file with last 500 products
How can I modify the script so it will make all these files automatically, so that I don't have to change each time the link manually?
我怎样才能修改脚本,让它自动生成所有这些文件,这样我就不必每次手动更改链接?
I would like to click a button which will do this:
我想单击一个按钮来执行此操作:
make the product0.txt
file with the first 500 products
product0.txt
使用前 500 个产品制作文件
wait 5 seconds
等待 5 秒
make the product1.txt
file with with another 500 products
product1.txt
使用另外 500 个产品制作文件
wait 5 seconds
等待 5 秒
make the product2.txt
file with the last 500 products
product2.txt
使用最后 500 个产品制作文件
回答by Brian
use:
用:
sleep(NUMBER_OF_SECONDS);
回答by genesis
before starting your actions, use
在开始你的行动之前,使用
sleep(5);
回答by Lawrence Cherone
or:
或者:
usleep(NUMBER_OF_MICRO_SECONDS);
回答by Ari Waisberg
In Jan2018 the only solution worked for me:
2018 年 1 月,唯一对我有用的解决方案是:
<?php
if (ob_get_level() == 0) ob_start();
for ($i = 0; $i<10; $i++){
echo "<br> Line to show.";
echo str_pad('',4096)."\n";
ob_flush();
flush();
sleep(2);
}
echo "Done.";
ob_end_flush();
?>
?>
回答by Khaled Developer
i use this
我用这个
$i = 1;
$last_time = $_SERVER['REQUEST_TIME'];
while($i > 0){
$total = $_SERVER['REQUEST_TIME'] - $last_time;
if($total >= 2){
// Code Here
$i = -1;
}
}
you can use
您可以使用
function WaitForSec($sec){
$i = 1;
$last_time = $_SERVER['REQUEST_TIME'];
while($i > 0){
$total = $_SERVER['REQUEST_TIME'] - $last_time;
if($total >= 2){
return 1;
$i = -1;
}
}
}
and run code =>
并运行代码 =>
WaitForSec(your_sec);
Example :
例子 :
WaitForSec(5);
ORyou can use sleep
或者你可以使用睡眠
Example :
例子 :
sleep(5);
回答by symcbean
I am on shared hosting, so I can't do a lot of queries otherwise I get a blank page.
我在共享主机上,所以我不能做很多查询,否则我会得到一个空白页面。
That sounds very peculiar. I've got the cheapest PHP hosting package I could find for my last project - and it does not behave like this. I would not pay for a service which did. Indeed, I'm stumped to even know how I could configure a server to replicate this behaviour.
这听起来很奇特。我已经为我的上一个项目找到了最便宜的 PHP 托管包 - 但它的行为并非如此。我不会为这样做的服务付费。事实上,我什至不知道如何配置服务器来复制这种行为。
Regardless of why it behaves this way, adding a sleep in the middle of the script cannot resolve the problem.
不管它为什么会这样,在脚本中间添加睡眠都不能解决问题。
Since, presumably, you control your product catalog, new products should be relatively infrequent (or are you trying to get stock reports?). If you control when you change the data, why run the scripts automatically? Or do you mean that you already have these URLs and you get the expected files when you run them one at a time?
因为,大概是你控制你的产品目录,新产品应该相对较少(或者你想获得库存报告?)。如果您控制更改数据的时间,为什么要自动运行脚本?或者您的意思是您已经拥有这些 URL,并且一次运行它们时您会得到预期的文件?
回答by Darwin
In https://www.php.net/manual/es/function.usleep.php
在https://www.php.net/manual/es/function.usleep.php
<?php
// Wait 2 seconds
usleep(2000000);
// if you need 5 seconds
usleep(5000000);
?>