views:

32

answers:

3

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 browser 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.

+1  A: 

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.

Daniel Hanly
I must add, this is an inadvisable action because if you have an internet outage as @Victor Nicollet suggests, then you would have no data send. Why can't you send immediately?
Daniel Hanly
Your example is great too in some situation, and thanks for `onbeforeunload` I never know that this exists :D
CIRK
Cheers mate, but after you added the travian example, this solution isn't good enough. At least now you can keep this knowledge for now :)
Daniel Hanly
+1  A: 

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()
Victor Nicollet
Maybe in PHP exclusively, but there are javascript functions that allow it.Web development these days is very much a combination of different languages and methods so using <script> </script> inside a PHP page is acceptable
Daniel Hanly
@Daniel Hanly: what is the JavaScript function that lets you send data to a server during a power outage or internet connection failure?
Victor Nicollet
You make a good point, but you perhaps shouldn't suggest that something is impossible. Perhaps inadvisable, but not impossible.
Daniel Hanly
Well I was thinking something very similar in my mind, however I didn't know what is it. This is a really good idea.
CIRK
@Daniel Hanly: did you read the whole answer? While this is impossible with *only* JavaScript, @Victor Nicollet has suggested a perfectly viable solution in the second paragraph.
Piskvor
I say it's impossible because there is no reliable way to have client-side code (such as JavaScript) run for 30 seconds after the user has left the page. All solutions posted so far have the client send the data (unreliably) when the page is left, which leaves the server with the task of waiting 30 seconds before inserting the data. At that point, sending the data straight away and handling the delay in the database is more reliable and does not require any client-side code.
Victor Nicollet
@Victor Nicollet: I have a feeling that's exactly what happens in the first example (Travian): the request is sent, (target time is inserted into db), page refreshes, user is shown countdown *to a time that's already in the database*
Piskvor
@Piskvor Before the OP added his travian and blogging example I had assumed that he meant almost like a "1 minute confirmation time" of a form submission, for which @Victor's answer wouldn't be the best solution. However, now the OP has added those examples, I would now offer the same solution as Victor, but it's too late to take away my down vote. I still don't agree with suggesting things are impossible, but his solution is Valid. Sorry mate!
Daniel Hanly
A: 

there is no guarantee for this action. I can leave page with closing browser too. Building database-driven solution is the best.

Burçin Yazıcı