PHP - sleep() 以毫秒为单位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47067127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:31:44  来源:igfitidea点击:

PHP - sleep() in milliseconds

phpsynchronizationsleep

提问by Navaneeth Mohan

Does PHP provide a function to sleep in milliseconds?
Right now, I'm doing something similar to this, as a workaround.

PHP 是否提供以毫秒为单位休眠的函数?
现在,我正在做类似的事情,作为一种解决方法。

$ms = 10000;
$seconds = round($ms / 1000, 2);
sleep($seconds);

I'd like to know whether there is a more generic function that is available in PHP to do this, or a better way of handling this.

我想知道 PHP 中是否有更通用的函数可以做到这一点,或者有更好的方法来处理这个问题。

回答by Scuzzy

This is your only pratical alternative: usleep - Delay execution in microseconds

这是您唯一实用的选择:usleep - 以微秒为单位延迟执行

So to sleep for two miliseconds:

所以要休眠两毫秒:

usleep( 2 * 1000 );

To sleep for a quater of a second:

睡四分之一秒:

usleep( 250000 );


Note that sleep()works with integers, sleep(0.25)would execute as sleep(0)Meaning this function would finish immediatly.

请注意,sleep()适用于整数,sleep(0.25)将执行为sleep(0)意味着此函数将立即完成。

$i = 0;
while( $i < 5000 )
{
  sleep(0.25);
  echo '.';
  $i++;
}
echo 'done';