Can you make a type of "setInterval" in PHP?

2

To run a particular function in a time interval.

    
asked by anonymous 08.10.2016 / 04:23

1 answer

5

PHP does not have this native. It does not even make sense. When you need something like this and PHP applications rarely need this, in addition to being common for programmers to opt for the wrong mechanism for their need, it is best to schedule the task in the operating system to call the desired script . Leave something running and firing will not work well, or even work, in web environment there is a short time limit that the script can run *

But if you want to know you can create a function that does this, alfo like this:

function setInterval($callback, $milliseconds) {
    $seconds = (int)$milliseconds / 1000;
    while (true) {
        $callback();
        sleep($seconds);
    }
}
    
08.10.2016 / 04:33