View Full Version : timer??

11-15-2010, 04:47 PM
Hi I am working on a chat application and am a bit stuck...In the database I have a field status which is set 1 for online and 0 for offline (PHP/MySQL)(at time of logging in/out), this shows the users status on a webpage. What I need is to know if there is a way I can set a timer so that if the user has been inactive for X amount of time, the database is auto updated and user is offline (1 set to 0).
Thanks for any help I recieve :)

Philip M
11-15-2010, 06:23 PM
See:- http://stackoverflow.com/questions/572938/force-logout-users-if-users-are-inactive-for-a-certain-period-of-time

You should do this server-side as the user may have Javascript disabled.

Here is a Javascript timer which you may care to use to advise the user of the count-down:-


<body onload ="display(601)">

<div id = "time"></div><br>
<input type = "button" value = "Click Here Or Press Any Key To Cancel Inactivity Timer" onclick = "cancel()">

<script type = "text/javascript">

document.onkeydown = function cancel() {
display(601); // 10 minutes

var tim;
function display(secs) {
if (secs <= 0) {
alert("Your time is up!");
document.getElementById("time").innerHTML = "If you are inactive you will be logged out in " +
Math.floor(secs/60) + " mins " + (secs % 60 < 9 ? "0" : "" ) + (secs % 60) + " secs";
tim = setTimeout("display("+secs+")",1000);


</html>All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Old Pedant
11-15-2010, 09:47 PM
There's actually a much better and easier way for something like a chat application: Instead of just an on/off flag in the DB, use a DATETIME field.

Every time the user makes a chat post, update that field to the current time. (You can also update it periodically if the user has some other activity indicating they are still "on" the site, of course.)

And now, to find users who are online, you simply SELECT all those whose last update in that field is with the last NN minutes, where you get to choose what NN is.

If you put an index on the field, this is just as efficient as selecting for a True/False "is online" field.