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('h:i:s') . "\n"; // sleep for 10 seconds sleep(10); // wake up ! echo date('h:i:s') . "\n"; ?>
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!