menu

arrow_back How do I set a certain amount of time to execute one step of the cycle?

by
1 vote
I need to write a loop so that each iteration of the loop takes a certain amount of time to process. In other words, I need to slow down the loop. So that in 1 second not more than 25 iterations are processed. I don't know how to do this.

4 Comments

No need to guess why, the authors' answers can always surprise. If you need trotlining, then, as already written - usleep. If something else, it is better to describe the purpose.
tukreb ,
The developer scattered sleep over all the functions and coded many of them.
Uh, what's the point? If he was mining crypto at the same time, that would be understandable.

Kirill Leonov And yet the question of "why" is still relevant.
FanatPHP Probably protection against scams, like slowing down the site if you quit :) Somewhere there was a case where a developer scattered sleep over all the functions and coded many of them.
Why

1 Answer

by
 
Best answer
0 votes
You can use sleep() or usleep() at the end of the cycle to delay the next iteration by a specified number of seconds or microseconds

foreach ( $variable as $key => $value ) {
# code...
sleep(10); // 10 секунд
usleep(200000); // 0.2 секунды
}