views:

79

answers:

4

Hello everyone! I've been looking around here on SO and Googling, however I can't find anything that fits my description.

What I want to do is update the database if the page has not been refreshed after 30 seconds. I want to email a person with the contents of a form {submitted by a different user} (I can do that) IF the person has NOT visited the page (I can do that) within the last 30 seconds.

What I've tried to do is make the page that should be visited refresh every 30 seconds, and so I figured if I did something like after 31 seconds, edit the database (so if the refreshed page was not refreshed, the database editing would run).

I'm sorry if this sounds complicated, there's probably a better way to do this, but I'm not sure how.

The bigger picture is I'm trying to make a 'on-duty' sort of thing, so that if the person is not actively looking at the page, they will get emailed with whatever the contents of the form is. The page will contain a table of all the entered form results.

A: 

maybe use an AJAX call every 30 secs based on the setTimeout javascript function?

http://www.w3schools.com/js/js_timing.asp

function timedCount()
{
    # ajax call
    t=setTimeout("timedCount()",30000);
}
Lizard
A: 

I can't provide php solution for the server-side part of the job. But basically you need to set up javascript timer (eg. jquery timeout) and after 30seconds do the ajax call that will do what you want on the server (save something to db, send email and so on).

I hope I got your point and my advice will help you somehow.

Lukasz Dziedzia
A: 

I would suggest using javascript like this:
window.setTimeout("location.reload(true);", 3000);

This is of course if you desperately need to reload, but the user will obviously be mad that you reload the window for him.

nillls
Youu can do this without realoading the page
Lizard
+2  A: 

You could update the database by creating a record with a timestamp every time the user refreshes the page. Then, you can have a PHP worker that looks regularely in the database if the timestamp is older than 30 seconds, and starts the e-mail.

small_duck
Ah yeah, that is an easier way to go about it.
Sam
Thankyou. I've now got the date. date("m/d/Y h:i:s a", time());How can I add 30 seconds on to that?
Sam
I think I'd do all the date time inside Mysql, which will give you more consistency: on refresh, you do something like "update last_refresh set timestamp = NOW() where user = ...", and to check if you need to send an e-mail you do "select user from last_refresh where DATEDIFF(NOW(), timestamp) > (30 / (3600 * 24))" (untested, I'm more a Postgresql person myself :) )
small_duck