How can I determine the set function execution time? Function & nbsp; (){... return & nbsp; true;} functionb (){... return & nbsp; false;} call a () now. if a () does not return results after 1.2 seconds, call B () immediately (); what are some beautiful solutions? ------ Solution-how to determine the set function execution time?
Function (){
//...
Return true;
}
Function B (){
//...
Return false;
}
Call ();
Imagine that if a () does not return results after 1.2 seconds, B () is immediately called ();
What are some beautiful solutions?
------ Solution --------------------
Don't be difficult for yourself or PHP
My solutions
1. Disadvantages of ajax timeout interpretation: time is not accurate enough. Advantage: This function can be implemented temporarily
2. Disadvantages of C language extension in PHP: difficult and cost-effective; advantages: accurate time and high efficiency
3. sit down and wait for the ox ....
------ Solution --------------------
Libevent can implement the timer function, but your a function should be used to execute network tasks. If yes, there must be a timeout setting.
------ Solution --------------------
Php itself provides such features
Pretty or not, you can judge by yourself
Not all operations are calculated in the php runtime. verify it by yourself.
Register_shutdown_function ("time_out_callback ");
Set_time_limit (2); // assume that a timeout occurs in 2 seconds.
// Simulate timeout
@ C ();
// Error_reporting ($ es); // Restore Settings
Function time_out_callback (){
If (connection_status () = 2 ){
B ();
}
}
Function c (){
// Your possible timeout code
Echo _ FUNCTION __,'';
While (true ){
Echo '.';
// Echo connection_status ()."
"; // Echo 0
Usleep (100000); // breathe the cpu
}
}
Function B (){
Echo'
'. _ FUNCTION __,'';
Echo 'c timeout connection status: '. connection_status (); // echo 2, c timeout
}