Does PHP provide a function to sleep in milliseconds?
Right now, I'm doing something similar to this, as a workaround.
$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.
The difference between sleep() and usleep() is that sleep() takes a number of seconds as its parameter, whereas usleep() takes a number of microseconds - millionths of a second - as its parameter.
The sleep() function delays execution of the current script for a specified number of seconds. Note: This function throws an error if the specified number of seconds is negative.
The wait function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function.
Delays the program execution for the given number of seconds . Note: In order to delay program execution for a fraction of a second, use usleep() as the sleep() function expects an int. For example, sleep(0.25) will pause program execution for 0 seconds.
This is your only pratical alternative: usleep - Delay execution in microseconds
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.
$i = 0;
while( $i < 5000 )
{
sleep(0.25);
echo '.';
$i++;
}
echo 'done';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With