A method of implementing timed execution function based on sleep function in PHP

Source: Internet
Author: User
Tags echo date sleep function
In PHP, there is a sleep function, which presumably means that the program executes when it encounters the sleep function and pauses for n seconds after execution. As sleep (10) means that the program is executed from the top down, after encountering the sleep (10) statement pause 10 seconds, and then continue to execute

In some guessing site, if we need to do a timed execution function, such as a problem, in 10 seconds to complete, otherwise display "You have timed out", if completed, then jump to the next topic above, and this in the middle of a 10-second pause, such a function is how to achieve it?

In PHP, there is a sleep function, which presumably means that the program executes when it encounters the sleep function and pauses for n seconds after execution. As sleep (10) means that the program is executed from the top down, after encountering the sleep (10) statement pause for 10 seconds, and then continue to execute down. The argument within the parentheses of the function is a numeric value that represents the pause time, in seconds. Take a look at the following code:

<?php//Current Timeecho date (' H:i:s '). "\ n";//sleep for ten secondssleep;//Wake Up!echo date (' H:i:s '). "\ n";? >

The results of the above program execution are:

05:31:23

05:31:33

Some children's shoes may be the example of the time will say how my program execution error, prompt timeout. This problem does not panic, this is the default PHP page execution time caused by default in PHP execution page time is 30 seconds, which is sufficient for the general program. But if you want to do something like timed execution, you must then set the execution time Set_time_limit (0) in the head declaration. 0 is the representative of the time, the unit is the second.

If the execution time is more than 30 seconds, then do the operation remember to connect to MySQL again, or invalid execution!!! The reason is that the database connection may be disconnected after the execution time is too long, and the database information cannot be read!

To do it, add a paused feature to your instance, and then proceed down.

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.