Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: timer??

  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Thanked 0 Times in 0 Posts


    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,573 Times in 2,551 Posts
    See:- http://stackoverflow.com/questions/5...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);
    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 11-15-2010 at 08:17 PM.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,732 Times in 4,694 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


    LinkBacks (?)

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts