Php, wait 5 seconds before executing an action

2020-02-24 07:31发布

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.

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

wait 5 seconds

make the product1.txt file with with another 500 products

wait 5 seconds

make the product2.txt file with the last 500 products

标签: php
7条回答
一纸荒年 Trace。
2楼-- · 2020-02-24 07:35

or:

usleep(NUMBER_OF_MICRO_SECONDS);
查看更多
淡お忘
3楼-- · 2020-02-24 07:35

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.

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?

查看更多
Fickle 薄情
4楼-- · 2020-02-24 07:39

Examples : Using sleep to add delay or increase execution time

Let us print the time after a delay of 15 seconds.

<?php
echo date('H:i:s');
sleep(15);
flush();
echo "<br>";
echo date('H:i:s');
?>

Output

11:36:19

11:36:34
查看更多
走好不送
5楼-- · 2020-02-24 07:53

before starting your actions, use

 sleep(5);
查看更多
够拽才男人
6楼-- · 2020-02-24 07:53
<?php
    foreach($i = 0; i < 500; $i++){
       your_code;
       delay(3); // 3 seconds
    }
?>
查看更多
走好不送
7楼-- · 2020-02-24 07:56

In Jan2018 the only solution worked for me:

<?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();

?>

查看更多
登录 后发表回答