Home >Backend Development >PHP Tutorial >Method to implement scheduled execution function based on sleep function in PHP

Method to implement scheduled execution function based on sleep function in PHP

墨辰丷
墨辰丷Original
2018-06-01 14:51:551692browse

In PHP, there is a sleep function, which probably 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 to execute.

In some betting websites, if we need to make a timing The function to be executed, for example, there is a question that must 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 will be a ten-second pause in the middle. Such a function How is it achieved?

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";
?>

The execution result of the above program is:

05:31:23

05:31:33

Maybe some When doing examples, Tongxiao will say why my program execution error occurred, 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.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP array addition operation and the difference from array_merge

##PHP uses the curl function to send Post requests Notes

php Source code for converting images into data/base64 data stream detailed explanation

The above is the detailed content of Method to implement scheduled execution function based on sleep function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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