please tell me how to make a delay function to delay functions!
DelayCommand(functionToDelay, Delaytime);
..? in php 5.3+
thanks for any help
function delayCommand($callback, $delayTime) {
sleep($delayTime);
$callback();
}
This should work, consider switching out sleep()
to usleep()
.
function DelayCommand($functionToDelay, $delayTimeInSeconds) {
sleep($delayTimeInSeconds);
$functionToDelay();
}
DelayCommand(function() { echo "yes"; }, 5);
(Code is untested)
function delayCommay($function, $nano){
usleep($nano);
$function();
}
Will do the trick however it is synchronous. So if you make a call to delayCommand
it will delay your whole script until it has run the command.
If you want it done asynchronously, see my answer here: Scheduling php scripts
For your information, here's a list of related functions:
sleep()
/ usleep()
- Sleep for an amount of (micro)seconds.time_sleep_until()
- Sleep until a timestamp.time_nanosleep()
- Sleep for an amount of seconds and nanoseconds.Here is what I have for delaying a function in MS, Sleep and Usleep pause the execution of the whole script, this seems to work pretty well
public function DelayTime($ms){
$now = microtime();
$finishtime = ($now + $ms);
while($now < $finishtime){
$now = time();
if($now >= $finishtime){ break; }
}
return true;
}