ewannnn Posted November 15, 2010 Share Posted November 15, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/218735-javascript-timer/ Share on other sites More sharing options...
upp Posted November 15, 2010 Share Posted November 15, 2010 you would probably have to do this using AJAX. set a timer so that after x amount of time a php script is ran that updates the users status as offline Quote Link to comment https://forums.phpfreaks.com/topic/218735-javascript-timer/#findComment-1134480 Share on other sites More sharing options...
Wildbug Posted November 17, 2010 Share Posted November 17, 2010 Exactly what upp said. The function you want is window.setTimeout. I'm not sure how your application is set up, but you could set a keydown/keypress listener to reset a timer that will call a function that will set the session as inactive. Here's a simple example. Delay is in milliseconds. var timer, delay = 15 * 60 * 1000, active = true; function makeInactive() { active = false; // update the server here } function keyDownListener() { if (timer) clearTimeout(timer); if (!active) { active = true; // update the server here } timer = setTimeout(makeInactive, delay); } keyDownListener(); // start the timer explicitly // you'll also need to attach the listener to the document. Quote Link to comment https://forums.phpfreaks.com/topic/218735-javascript-timer/#findComment-1135524 Share on other sites More sharing options...
seanlim Posted November 17, 2010 Share Posted November 17, 2010 Just a thought to add on. You might want to store the "last active time" for each user in the database, and use database functions to see which users should be logged out. This is because users who do not explicitly logout (ie close window/crash) will always be seen as online until they/javascript logs out the next time they visit. Quote Link to comment https://forums.phpfreaks.com/topic/218735-javascript-timer/#findComment-1135546 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.