Use the sleep function in PHP to implement the code of the scheduled execution function.

Source: Internet
Author: User
Tags echo date sleep function

Use the sleep function in PHP to implement the code of the scheduled execution function.

In some quiz websites, if we need to implement a regular execution function, such as a question, it will be completed within 10 seconds; otherwise, "you have timed out" will be displayed, then I jumped to the next question, and there was a 10-second pause in the middle. How was this function implemented?

In PHP, there is a sleep function, which probably means that when a program runs a sleep function, it is paused for N seconds and continues to be executed. For example, sleep (10) means that the program runs from top to bottom. When the sleep (10) Statement is run, it is paused for 10 seconds and then continues to run. The parameter in the function brackets is a numerical value, indicating the pause time value, in seconds. See the following code:

<? Php // current timeecho date ('H: I: s'). "\ n"; // sleep for 10 secondssleep (10); // wake up! Echo date ('H: I: s'). "\ n";?>

The execution result of the above program is:

05:31:23

05:31:33

Some children's shoes may say how to execute an error when making an instance, prompting timeout. Don't be alarmed when this problem occurs. This is caused by the default page execution time of PHP. in PHP, the default page execution time is thirty seconds, which is enough for general programs. However, if you want to perform a scheduled execution function like this, you must declare the execution time in the header and set set_time_limit (0 ). 0 indicates the time limit, in seconds.

If the execution time exceeds 30 seconds, you must connect to MYSQL again before performing the operation. Otherwise, the execution is invalid !!! The reason is that the database connection may be disconnected after the execution time is too long, so the database information cannot be read!

Click here to add a pause function for your instance, and then execute it.

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.