PHP timer function

Asked

Viewed 847 times

-1

Does anyone know any timer function like javascript setTimeOut: https://www.w3schools.com/jsref/met_win_settimeout.asp

Only in PHP?

Because I’m trying to do it with Sleep, but the page keeps loading endlessly.

function Criar_Madeira($a) {
        $a->madeira += 500;
        sleep(10);
        Criar_Madeira($a);
}

Criar_Madeira($a[1]);
  • Your function never ends because you have created a function with recursiveness. Don’t forget that PHP is a server side language, that is while your PHP script is not completed you will get "no" server response.

  • 1

    What was the result you expected to achieve with a "timer" on the PHP side ? If the idea is to give some timing to the actions the user does then it’s javascript you want to use.

  • Or use a process in background powered by CRON.

  • Anderson, that would be replace Sleep by usleep, because I saw it on the net, I did it and keep loading endlessly.

2 answers

0

PHP is server-side, so this can only be done on the client side, in case, javascript.

  • And Jquery, Angular, etc ?

  • But this is for a game, if you have in client side it will be possible to steal...

0

You called the function inside the function, and when you do that, you’re going to keep giving it an infinite loop, you know? Because she will run the same all over again, and will end up running the part she is called again rs.

function criarMadeira($a) { //Por que precisa desse parâmetro?
    sleep(10);  
    $this->madeira += 500;
}

criarMadeira(1); //Não entendi o porque desse valor 1

Remember that PHP also works in cascade, things run from top to bottom. With Sleep before, it will run the $this->madeira += 500; only after the same.

  • It’s really supposed to be an infinite loop, smpre keep adding wood. .

  • Dude, so the page will crash... It will always crash. It’s supposed to be infinite anyway?

Browser other questions tagged

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