Does PHP have the function of executing the task several seconds later? What should I do?

Source: Internet
Author: User
Does PHP have the function of executing the task several seconds later? For example, if there is a code segment, first execute the first line, and then execute the second line after 5 seconds. Note that, instead of a loop, the code is executed in the order from top to bottom, wait a few seconds in the middle ------ solution ------------------ sleep (5); ------ solution ------------------ sleep (5) program pause 5 seconds ------ solution ------- does PHP have the function of executing after several seconds?
For example, if there is a code segment, first execute the first line, and then execute the second line after 5 seconds.

Note that the code is executed in the order from top to bottom, instead of a loop. the pause takes several seconds.

------ Solution --------------------
Sleep (5 );
------ Solution --------------------
Sleep (5) program pause for 5 seconds
------ Solution --------------------
The previous 1 is in the output cache of the server, or your browser z settings are incorrect. But in any case, you waited for at least five seconds to see the output!
PHP code
Echo microtime (); // Current timestamp sleep (5); echo"
"; Echo microtime ();
------ Solution --------------------
For browser reasons, you can see the effect of the pause when debugging with CLI.
If it is only for the display effect of the pause, it is better to hand over to the front-end js for processing. if the program is running in the background, you must believe that the middle of the two lines must have paused for 5 seconds.
------ Solution --------------------
Discussion

It seems a problem.

PHP code
Echo '1 ';
Sleep (5 );
Echo'
5 ';


Each refresh appears to be both 1 and 5.

------ Solution --------------------
How many times does it need to be executed every five seconds?
If it is an infinite loop, use SHELL or CRON to control the execution of PHP. Generally, PHP script execution time should not exceed 60 seconds. Otherwise, the script may be stuck.
------ Solution --------------------
Sleep (5)
------ Solution --------------------
Echo microtime (); // Current timestamp
Flush ();
Sleep (5 );
Echo"
";
Echo microtime ();

In this way, you will see the effect in the browser.
------ Solution --------------------
Meaningless work, not to mention that all the code above is problematic, even if there is no problem with the code, it can only be achieved by using a specific text server with a specific browser.

Everyone (including Dummies) thinks that the Internet is too slow. Some people intentionally slow down the network response speed.

------ Solution --------------------
PHP is mainly used to generate web pages. a web page is always generated once and then transmitted to the browser. it cannot be transmitted at. Otherwise, how many pages will be transmitted at one byte? Only by setting PHP as CMD command line and CGI can we achieve arbitrary pause and intermittent display of content like a desktop program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.