Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - sleep() in milliseconds [duplicate]

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.

like image 534
Navaneeth Mohan Avatar asked Nov 02 '17 03:11

Navaneeth Mohan


People also ask

What is the difference between sleep and Usleep?

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.

How does sleep work in PHP?

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.

Is there a wait function in PHP?

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.

How do I put PHP to sleep?

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.


1 Answers

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';
like image 143
Scuzzy Avatar answered Oct 14 '22 17:10

Scuzzy