Welcome Guest, Not a member yet? Register   Sign In
sleep then repeat function in controller
#1

[eluser]new_igniter[/eluser]
Hello,
Can anyone help me to understand the CI appropriate way to run a function, sleep it for 20 seconds, then start the function again.

I would imagine something like this, but it doesnt work.

Code:
function exampleFunction($sleepTime)
{
  do some code...

sleep($sleepTime);

$this->exampleFunction(20);

}
#2

[eluser]Bogdan Tanase[/eluser]
your code looks like it's going to cause a stack overflow, since it's a recursive function and it doesn't seem to end. Although I'm pretty sure it will time out first (script max_execution_time) Big Grin

Depending on your purpose I think you should use some sort of scheduling (crontab, or smth similar) to run your script at a specific interval, otherwise your script will run continuously, and eventually time out.

otherwise, if you want to run the script directly the correct way would be this:

Code:
while($condition==TRUE) //when you want to end the loop, set $condition to FALSE
{
  // do code
  sleep($sleepTime);
}
#3

[eluser]rogierb[/eluser]
You can use a redirect to prevent stack overflow:
Code:
function exampleFunction($sleepTime)
{
  //do some code...

  sleep($sleepTime);

  redirect("yourController/exampleFunction/20");

}
#4

[eluser]obiron2[/eluser]
What exactly are you tring to accompish with this.

If you are looking for a page refresh, it won't work becasue the client will not have requested the page so will not be expecting repeated output. You would need to do this client side with some javascript to refresh the page.



If you are trying to regularly schedule an activity, you would be better to call a function that checks the last time it was run and runs it again if X minutes have passed and add it to some of the controller calls from the client.




Theme © iAndrew 2016 - Forum software by © MyBB