How to determine the Set function execution time

Source: Internet
Author: User
How can I tell how to set the function execution time?
function A () {
//...
return true;
}
Function B () {
//...
return false;
}
Now start calling a ();

Imagine if a (), after 1.2 seconds did not return the result, immediately call B ();
What's the nice way to deal with it?


------Solution--------------------

Don't be too yourself, and don't make PHP any harder.

My solution
1, Ajax time-out interpretation disadvantage: Time is not accurate advantage: This feature can be temporarily implemented
2, PHP's C language extension disadvantage: The difficulty is big cost-effective not high advantage: time accurate efficiency high
3. Wait for Daniel ....
------Solution--------------------
Libevent can implement the function of the timer, but your a function should be to perform network tasks, if so, there must be a timeout setting
------Solution--------------------
PHP itself provides this type of functionality
Whether it is beautiful, you judge by yourself
Not all operations are calculated in PHP run time, you can verify it yourself
Register_shutdown_function ("Time_out_callback");
Set_time_limit (2);//Assuming 2 seconds to timeout
Analog timeout
@c ();
Error_reporting ($es);//Restore settings

function Time_out_callback () {
if (connection_status () = = 2) {
b ();
}
}
Function C () {
Your possible time-out code
echo __function__, ';
while (true) {
echo '. ';
Echo Connection_status (). "
";//echo 0
Usleep (100000);//give the CPU a breath
}
}
Function B () {
Echo '
' . __function__, ";
Echo ' C Timeout connection status: '. Connection_status ();//echo 2,c Timeout
}
  • 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.