Delay Function PHP

谁说我不能喝 提交于 2019-12-06 04:38:58

问题


please tell me how to make a delay function to delay functions!

DelayCommand(functionToDelay, Delaytime);

..? in php 5.3+

thanks for any help


回答1:


function delayCommand($callback, $delayTime) {
    sleep($delayTime);
    $callback();
}



回答2:


This should work, consider switching out sleep() to usleep().

function DelayCommand($functionToDelay, $delayTimeInSeconds) {
    sleep($delayTimeInSeconds);
    $functionToDelay();
}

DelayCommand(function() { echo "yes"; }, 5);

(Code is untested)




回答3:


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.




回答4:


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.



回答5:


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;
     }


来源:https://stackoverflow.com/questions/6803756/delay-function-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!