There is a function in php, let's say:

function pop($num) { //..... } 

How can I run it after a certain time interval? Is there an analog to the js function setTimeout in php?

  • 3
    Describe the task more precisely. If this is not the case when you can / need to call a PHP script on an asynchronous request from JS, then a cron-type scheduler will most likely work. It is incorrect to draw analogies between JS and PHP in this respect - two different languages ​​with different tasks and different environments. - oleg42

5 answers 5

It will not work for you as in JS. You have to use cron here

    can not. There is no setTimeout counterpart in PHP.

      I would do it like this.

      I assigned a variable to the function date (i) , indicated the format, and then compared it with a condition (for example, minutes), if it is true, the function would be executed.

      PS At the same time the page should reload every time.

      Therefore...

      ... In general, it is better to use JS.

      • And how can you combine js timer and php code? - maza51

      PHP has such a great function as sleep .

      • Ie, I now do this: function pop ($ num) {// ..... sleep (2); pop ($ num); } Right? But it does not work .. - maza51
      • 3
        If you do, even without trying to understand what will happen when you try to execute the code, it will not work) - Sh4dow
      • one
        Then function pop ($ num) {// function body return $ continue; // continue? } while (pop (get_mystic_value ())) {// show must go on sleep (1); } - oleg42
      • one
        sleep () - dope =) - AseN
      • Thanks guys. Yes, everything turned out. I just thought it would work differently) - maza51

      here you need to use the header header

       <?php header('Refresh:1'); echo date('dmY H:m:s'); ?> 

      The type of watch is obtained, but the whole page will be updated.