Home > php教程 > PHP开发 > Implementation code for timing execution function using sleep function in PHP

Implementation code for timing execution function using sleep function in PHP

高洛峰
Release: 2016-12-23 12:50:04
Original
1226 people have browsed it

In some betting websites, if we need to do a scheduled execution function, for example, there is a question that needs to be completed within ten seconds, otherwise it will display "You have timed out". If it is completed, it will jump to the next question. , and there is a ten-second pause in the middle. How is this function implemented?

In PHP, there is a sleep function, which roughly means that when the program execution encounters the sleep function, it pauses for N seconds and then continues execution. For example, sleep(10) means that the program is executed from top to bottom. After encountering the sleep(10) statement, it pauses for ten seconds and then continues execution. The parameter in the function brackets is a numerical value, representing the pause time value, in seconds. Please look at the following piece of code:

<?php
// current time
echo date(&#39;h:i:s&#39;) . "\n";
// sleep for 10 seconds
sleep(10);
// wake up !
echo date(&#39;h:i:s&#39;) . "\n";
?>
Copy after login

The execution result of the above program is:

05:31:23

05:31:33

Maybe some children’s shoes will say that my program execution error occurs when doing examples, prompting a timeout. . Don’t panic if this problem occurs. This is caused by PHP’s default page execution time. The default page execution time in PHP is thirty seconds, which is enough for general programs. But if you want to do a similar scheduled execution function, you must set the execution time set_time_limit(0) in the header statement. 0 means no time limit, the unit is seconds.

If the execution time exceeds 30 seconds, remember to connect to MYSQL again before performing the operation, otherwise the execution will be invalid! ! ! The reason is that the database connection may be disconnected after the execution time is too long, and the database information cannot be read!

Go and do it, add a pause function to your instance, and then proceed.

For more articles related to using the sleep function to implement scheduled execution function code in PHP, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template