How To: Generate a random pause delay in your PHP script

If you have a script that requires a random pause delay --- for example, perhaps a web scraper --- here's a super simple way to do it.

usleep(rand(1000,3000));

The rand() function is being fed two numbers; the lowest number to generate, and the highest.

The usleep() function causes a script to pause for a specified number of microseconds.

So the code above is saying "randomly sleep between 1000-3000 microseconds", which means 1-3 actual seconds.

Simple, and effective!

October 6, 2011 at 2:02am / General Web Programming, PHP 

Comments On This Entry

So excited I found this article as it made thgins much quicker!

Jory @ 10:47:40 AM, Sunday, October 23, 2011

Compose New Blog Comment
    required = required field         warning HTML and URL's are not allowed

[back to main]