S
S
Schoolboy.2015-09-27 20:34:19
PHP
Schoolboy., 2015-09-27 20:34:19

How to make a loop that repeats every second?

How to make a loop that repeats every second?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
AlikDex, 2015-09-27
@viphorizon

for (;;) {
    sleep(1);
}

I
IvanSenishin, 2015-09-27
@IvanSenishin

while(true){ // запускаем бесконечный цикл
    sleep(1); // задержка в 1 секунду
}

K
Konstantin Muzhikov, 2015-09-27
@GriSII

just do not forget that the cycle + sleep(1) in one hour will be executed not 3600 times, but less, since the cycle time will take 1 sec + the time for performing operations in the cycle.
For some tasks, this is not critical, but for example, programming microcontrollers (I encountered this) is quite critical.
And we must not forget that if you work with peripherals (serial ports, usb, requests to external web servers), the operation time is relatively large.
In this case, one could contrive to track the execution time of operations in a loop (microtime in php for example) and subtract this time from a second, or even bind to the server clock (to be honest, I did this only in c # and then as an experiment)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question