2

Possible Duplicate:
Is there a function similar to setTimeout() (JavaScript) for PHP?

Is there any way to do a timeout in PHP? Something similar to setTimeout() in Javascript. I want to insert data in database after 5 minutes.

Edit: Also. It will do another things after 5 minutes. A function will be executed. Then, the approach shouldn't be with the database. A hang is undesirable

Community
  • 1
  • 1
vicenrele
  • 971
  • 3
  • 19
  • 37

6 Answers6

4

If the database does not already have a CURRENT_TIMESTAMP field, add one. Then, insert the row immediately but set that CURRENT_TIMESTAMP field to DATE_ADD(NOW(),INTERVAL 5 MINUTE).

Then, in whatever code you have SELECTing from this table, include WHERE ts <= NOW() (where ts is the name of your CURRENT_TIMESTAMP field)

I use this technique in one of my Cron scripts to simulate it running non-stop when actually it's running once every 15 minutes, by using a random interval between 0 and 15 minutes, and it works really well.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

You'll need to use something like a cron job or task scheduler depending on where you are hosted. PHP is not event driven.

keeg
  • 3,990
  • 8
  • 49
  • 97
1

Like others said, there is no "clean" way. You better use a cron job or scheduler-like utility provided by the environment in which you are hosted. And I strongly advise you not to use sleep().

An alternative would be the one of completely changing your approach. Why don't you put the data in the DB immediately, but give it a different "meaning" after 5 minutes?
e.g. you post a new row with a timestamp. When you read, you filter out all the rows that have been inserted after 5 minutes ago.

gd1
  • 11,300
  • 7
  • 49
  • 88
  • It hangs the script, which still runs but it is unresponsive. A bad time for HTTP clients :) – gd1 Jan 24 '13 at 18:38
  • It will do another things after 5 minutes. A function will be executed. Then, the approach shouldn't be with the database. Some idea? – vicenrele Jan 24 '13 at 20:03
0

The php sleep() function is the way to go. More details in this thread. In your case the code might be:

<?

sleep(300); // Or 'sleep(5*60);' to make it more readable.
echo 'Do something!';

?>
Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
0

Im not sure why you need this but you can use

sleep(300);

For more http://php.net/manual/en/function.sleep.php

But I would use a more elegant solution, again this is dependant on what you can or cannot do.

I would store the data in a temporary file. Then use cron to run a script every five minutes to check weather or not a data file exists. If it does, take the file and insert into the database.

Angel.King.47
  • 7,922
  • 14
  • 60
  • 85
  • 1
    `sleep(5000);`? 83.3333 minutes? – Giacomo1968 Jan 24 '13 at 18:02
  • 1
    You are right, late in the day :D – Angel.King.47 Jan 24 '13 at 18:03
  • Sleep will hang the script. It is not only inelegant, but unacceptable. – gd1 Jan 24 '13 at 18:04
  • 1
    Yes I know, I answered as he asked, hence why the offer for a more elegant solution underneath. – Angel.King.47 Jan 24 '13 at 18:05
  • 1
    @gd1 you really cannot say a solution is inelegant without knowing the full details. As the original poster stated they simply wanted to delay a script from inserting data into a database. Who knows what or why? `sleep()` makes sense given the parameters given by the original poster. – Giacomo1968 Jan 24 '13 at 18:13
  • `sleep()` hangs the script – vicenrele Jan 24 '13 at 20:09
  • @vicenrele that is what its designed to do, Again im not sure exactly why you want this. I think what you are after is threading, and PHP is not really the best language for this you can look here http://stackoverflow.com/questions/209774/does-php-have-threading. You can try to fork() a child and call your sleep() inside the child, so your parent is allowed to continue. – Angel.King.47 Jan 24 '13 at 21:14
  • my intention is to execute a function after 5 minutes. This function change a flag y database. I have a web application which the users can use it for 5 minutes in demo version (only one user both). In the client I use the page is closed with Javascript and sends an Ajax request to reset the flag. But if the user leaves of the page 5 minutes before, the flag isn't reset. For 5 minutes isn't so important, but if the demo is for 15 minutes, I think that something like is necessary – vicenrele Jan 24 '13 at 22:18
  • @Angel.King.47. Thanks for response. I can't try to fork() a child because Apache servers don't support `pcntl_fork()`. But It was a great idea. – vicenrele Jan 24 '13 at 22:57
  • You can fire an ajax request, when the browser is closed http://stackoverflow.com/questions/1704533/intercept-page-exit-event – Angel.King.47 Jan 25 '13 at 09:53
  • Ok. But not work if the client loses Internet connection – vicenrele Jan 25 '13 at 09:57
  • @vicenrele Am I right in assuming that you want a session to timeout, if the user is inactive for N minutes? – Angel.King.47 Jan 25 '13 at 10:29
  • No. The flag indicates if a user can do log in, because only one user both can use the application. When the user closes the demo, the flag should be reset also since php (not only by ajax request because it's possible that the user loses the Internet connection and ajax request isn't realized) . Then another user could user the demo application – vicenrele Jan 25 '13 at 10:46
  • Try this http://stackoverflow.com/questions/3164507/allow-one-session-only-at-a-time – Angel.King.47 Jan 25 '13 at 10:49
  • That question is for a unique user in different computers. The solution is the same that mine but without timeout. But timeout is my problem – vicenrele Jan 25 '13 at 11:22
-1

You can use AJAX to save data with the javascript setTimeout.

kwelsan
  • 1,229
  • 1
  • 7
  • 18