PHP : Delayed database request also without live page
So for example I click a button what will send something to the database after 1 minutes, but I want to send it also if I leave the page
:)
So if I leave the page when 30 seconds left from the 1 minutes the countdown will continue until reaches 60 seconds, then sends the data to the database.
Hope it's clear. I think that I should use for this SESSION
's but please let me know if you know the answer, or anything helpful :)
example: on http://www.travian.com/, when when you start building something, you get a countdown "will be finished in xx:xx:xx"; yet you don't need to keep the brow开发者_运维技巧ser open - the countdown still runs on the server, even if you log out.
Or lets say a real life example:
Imagine a blogging system like wordpress, where I write 10
posts a day but I want to show for the users only 5/day
.
Or I want to show to users at 8:00 A.M.
Or another one taken from travian:
Imagine a game
where you can create troops, but to create troops you need to wait because life's not easy :D, so for a troop
you need to wait 1 minutes
.
This is impossible. The user might leave the page because their internet connection failed or because there's a power outage, at which point there's no way to have them send the information to the database.
What you can do is store the data in the database immediately, with a "do not open before" timestamp to simulate a one-minute delay, and add the additional constraint to all requests against that table:
WHERE createDate < NOW()
Perhaps you want to use a javascript command to initiate the delay, because with Javascript you can send data to PHP via AJAX, and if you encapsulate this AJAX in a function and then call this from your "delay" function.
For the page leaving you could also use:
window.onbeforeunload=delayFunction;
Hope this helps.
there is no guarantee for this action. I can leave page with closing browser too. Building database-driven solution is the best.
精彩评论