1

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.

2
  • I don't understand your question. Can you rephrase? Commented Aug 31, 2010 at 8:48
  • I don't know how to do this. But I am sure, that this makes with help of cron. Commented Aug 31, 2010 at 8:49

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

8 Comments

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: what is the JavaScript function that lets you send data to a server during a power outage or internet connection failure?
You make a good point, but you perhaps shouldn't suggest that something is impossible. Perhaps inadvisable, but not impossible.
Well I was thinking something very similar in my mind, however I didn't know what is it. This is a really good idea.
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.
|
1

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.

3 Comments

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?
Your example is great too in some situation, and thanks for onbeforeunload I never know that this exists :D
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 :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.