0

I have a function where I want to delay a certain call in the middle, but I don't want to stop the whole function. Even after the last line I want the part I delayed to run after a designated time. Here's an example of what I mean:

 function delayInMiddle()
    {
    function call 1
    if(some condition met in 30 seconds)
            {function call 2}
    function call 3
    }

I want to have function 1 called, check for function 2, continue to function call 3, then go back and do function call 2 in 30 seconds. Is this possible in php?

hden
  • 21
  • 4
  • The answer depends upon what the process that you want to delay actually does. Can you tell us what `function call 2` will be doing at least in broad terms. Is this a web page or a batch script? – RiggsFolly Jul 30 '15 at 12:04
  • You can call function 2 at the end with a sleep of 30 seconds before calling it., Or If this will not solve your problem can you explain a little more what you are trying to do with this kind of logic so that we can help you in better way. – Tarun Upadhyay Jul 30 '15 at 12:04
  • Function call 2 is supposed to be a post to twitter. I'm not posting anything that's being passed into the function, just some general text. And if I call function 2 at the end it won't get executed because of the nature of function 3. – hden Jul 30 '15 at 12:06

2 Answers2

0

How about this as a possible solution.

Instead of calling function2 in that script, write the relevant data to a table that is operated as a queue + a time before which it should not process this queue item, that creates your x second delay.

Then make function 2 a cron job that runs every 30 seconds. It looks at the queue to see what if anything there is for it to do.

foreach ( row in queue ) {
     send twitter;
     delete row from table;
}
exit;
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-1

You can use sleep() for delay. http://php.net/manual/en/function.sleep.php

Disha V.
  • 1,834
  • 1
  • 13
  • 21