How To Use setInterval in PHP?

后端 未结 8 1922
刺人心
刺人心 2020-12-08 16:11

I want to ask that how can i use php function again and again after some time automatically just like setInterval in Javascript. We set the time and it is on its job until t

8条回答
  •  情歌与酒
    2020-12-08 17:05

    since the asynchronous concept of web development has to do with effecting the changes on a web page without reloading the page, we must not always run to the bays of Ajax when ever we need such effects on our web pages, PHP can simply do the job of going to the database @ sleep seconds to retrieve some sets of data for our usage especially for chat application purposes. See the below codes:

    function setInterval ( $func, $seconds ) 
    {
          $seconds = (int)$seconds;
          $_func = $func;
          while ( true )
          {
                $_func;
                sleep($seconds);
          }
    }
    

    Now, let's say we have a function get_msg() that goes to the database to download some sets of information, if we must do that repeatedly without the repeated usage of button calls, then, see the usage of the setInterval function written above with the get_msg function.

    setInterval ( get_msg(), 5 );
    

提交回复
热议问题