Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set a time limit for a code block?

Tags:

php

php-5.3

Is it possible to start a block of code (maybe just call a function) and if it doesn't execute within a certain time skip it.

//give this function 10 seconds to execute
$value = mega_function();// could take anything from 1-1000 seconds
//if 10 seconds have passed and the value is still not set, abort it and set $value = false;
like image 370
Moak Avatar asked May 11 '11 07:05

Moak


1 Answers

No. You would have to either

  • Call the function inside an external file using curl or file_get_contents() - you can set a timeout there

  • Keep track of the time inside mega_function() and return() if necessary.

What does mega_function() do?

like image 188
Pekka Avatar answered Oct 23 '22 12:10

Pekka