Using sleep function in PHP to implement the function of timing execution to realize code _php skill

Source: Internet
Author: User
Tags echo date numeric value sleep sleep function

In some of the website of quiz, if we need to do a function of timed execution, for example, a problem, in 10 seconds to complete, otherwise show "You have timed out", if completed, then jump to the next question above, and this middle has a 10-second pause, such a function is how to achieve it?

In PHP, there is a sleep function, presumably meaning that the program execution encounters the sleep function while pausing for n seconds before continuing. Sleep (10) means that the program is executed from the top down, the sleep (10) statement is paused for 10 seconds, and then the execution continues. The parameter in the function parentheses is a numeric value that represents the pause time, in seconds. Take a look at the following code:

<?php
//Current time
echo date (' H:i:s '). "\ n";
Sleep for ten seconds Sleep
(a);
Wake Up!
echo Date (' H:i:s '). "\ n";
? >

The results of the above program execution are:

05:31:23

05:31:33

may be some child shoes to do when the example will say how my program execution error, prompt timeout. This problem does not panic, this is the PHP default page execution time, in PHP, the default execution page time is 30 seconds, which is sufficient for general procedures. But if you want to do a function like timed execution, you must Set_time_limit (0) The execution time of the head declaration setting. 0 is the limit when the unit is seconds.

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

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

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.