0

I have a block of code and want to run it after certain time intervals, say after every 3 seconds. If I get required result it should die otherwise another request will be sent after 3 seconds. Any idea to do it with php.

Thanks in advance for any help

Altaf Hussain
  • 1,048
  • 2
  • 12
  • 25
  • If you get more than 1 hit every 3 seconds, you could do it when a visitor hits your site, but this is very dodgy. You really do need to use `cron`. – Bojangles Jun 05 '12 at 10:08
  • but i have to pass some parameters to the function according to users request. – Altaf Hussain Jun 05 '12 at 10:10
  • Any reason you dont wanna use cron? Only alternative is to have your users poll the server every 3 seconds via javascript and keep a check on last update but that would involve wasted resources. – Lawrence Cherone Jun 05 '12 at 10:12

2 Answers2

4

You can sleep in a loop:

while (true) {
    $result = doSomething;
    if (resultIsGood) {
        break;
    }
    sleep(3);
}

If you don't want to keep the browser waiting, you can look up ignore_user_abort() solutions on Google.

lanzz
  • 42,060
  • 10
  • 89
  • 98
1

If what you want to execute MySQL queries (and nothing else), maybe using the MySQL event scheduler could fit your need:
http://dev.mysql.md/doc/refman/5.5/en/events-overview.html

Jocelyn
  • 11,209
  • 10
  • 43
  • 60