当前位置: 动力学知识库 > 问答 > 编程问答 >

how to make a php function loop every 5 seconds

问题描述:

I am tyring to make a php function that updates every second using php itself no other languages, just pure PHP codes.

function exp(){

//do something

}

I want it to return a value each second. Like update every second.

网友答案:

It's not preferable to make this in PHP, try to make on client side by calculating difference between time you got from database and current time. you can make this in JS like this:

setInterval(function(){
      // method to be executed;
    },5000);  // run every 5 seconds
网友答案:

sleep() function is the function that you are looking for:

while (true) {
    function(); // Call your function
    sleep(5);
}
  1. While loop with always true
  2. Call your function inside while loop
  3. Wait for 5 seconds(sleep)
  4. Return the beginning of loop
网友答案:
function exp(){
//do something
}
while(true){
exp();
sleep(5);
}

Use sleep function to make execution sleep for 5 seconds

it will be better if you use setInterval and use ajax to perform your action

网友答案:

Suppose exp() is your function

function exp(){
 //do something
}

Now we are starting a do-while loop

$status=TRUE;

do { 

   exp(); // Call your function
   sleep(5);   //wait for 5 sec for next function call

   //you can set $status as FALSE if you want get out of this loop.

   //if(somecondition){
   //    $status=FALSE:
   //}

} while($status==TRUE); //loop will run infinite

I hope this one helps :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: