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

Asked

Viewed 1,759 times

3

To perform certain function in a time interval.

  • 1

    Easier would be to run a PHP from time to time, by linux crontab or windows scheduler.

  • 1

    It would help a little if the question brought more context e.g. what kind of activity is intended to do regularly.

  • @epx Thanks for your attention, but this question is more than 3 years old. I can’t remember the context in which I made it.

1 answer

9


PHP doesn’t have it native. Nor does it make sense. When you need something like this and PHP applications rarely need it, and it is common for programmers to opt for a wrong mechanism for their need, it is better to schedule task in the operating system to call the script desired. Leaving something running and firing will not work well, or even work, in a web environment there is a short time limit that the script can rotate

But if you want to know, you can create a function to do that, something like that:

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

I put in the Github for future reference.

But it’s too bad to do it, don’t do it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.