how to know how much time is left for a setTimeout?
basically what i am doing is set a timer to logout the user after a hour has passed but only if the user is inactive so i want to restart the timer after user has did any activity (scroll, keypress, mousemove, click) the thing is 开发者_如何学Cthat the timer doesn't reset or something else thats why in the logout function i want to know did the timer actually reset and thats why i want to know the time passed by the timer.
i am using javascript, any help pls?
If this is a "feature" -- sure use Javascript, but if security is your aim, use server side, no doubt about it.
It would be a big nuisance to have to code something to track every little change that could happen, scroll, click, focus, keypress, etc, to determine if the user was "idle" or not -- so it's an idea to use something like erichydns idleTimer, jquery plugin: http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/
edit: this would be more suitable for you: http://paulirish.com/2009/jquery-idletimer-plugin/
demo is available here which shows in real time if the user is "idle" or not http://paulirish.com/demo/idle-timer
You can clar the setTimeout using the following code:
var t = setTimeout(myFunction,100000);
clearTimeout(t); //clear the timeout
If you want to know how much time is left, then you have to count the seconds or so ;)
精彩评论