Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:49:56+00:00 2026-06-01T02:49:56+00:00

How can I track the browser idle time? I am using IE8. I am

  • 0

How can I track the browser idle time? I am using IE8.

I am not using any session management and don’t want to handle it on server side.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:49:58+00:00Added an answer on June 1, 2026 at 2:49 am

    Here is pure JavaScript way to track the idle time and when it reach certain limit do some action:

    var IDLE_TIMEOUT = 60; //seconds
    var _idleSecondsTimer = null;
    var _idleSecondsCounter = 0;
    
    document.onclick = function() {
        _idleSecondsCounter = 0;
    };
    
    document.onmousemove = function() {
        _idleSecondsCounter = 0;
    };
    
    document.onkeypress = function() {
        _idleSecondsCounter = 0;
    };
    
    _idleSecondsTimer = window.setInterval(CheckIdleTime, 1000);
    
    function CheckIdleTime() {
         _idleSecondsCounter++;
         var oPanel = document.getElementById("SecondsUntilExpire");
         if (oPanel)
             oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + "";
        if (_idleSecondsCounter >= IDLE_TIMEOUT) {
            window.clearInterval(_idleSecondsTimer);
            alert("Time expired!");
            document.location.href = "logout.html";
        }
    }
    #SecondsUntilExpire { background-color: yellow; }
    You will be auto logged out in <span id="SecondsUntilExpire"></span> seconds.

    ​This code will wait 60 seconds before showing alert and redirecting, and any action will “reset” the count – mouse click, mouse move or key press.

    It should be as cross browser as possible, and straight forward. It also support showing the remaining time, if you add element to your page with ID of SecondsUntilExpire.

    The above code should work fine, however has several downsides, e.g. it does not allow any other events to run and does not support multiply tabs. Refactored code that include both of these is following: (no need to change HTML)

    var IDLE_TIMEOUT = 60; //seconds
    var _localStorageKey = 'global_countdown_last_reset_timestamp';
    var _idleSecondsTimer = null;
    var _lastResetTimeStamp = (new Date()).getTime();
    var _localStorage = null;
    
    AttachEvent(document, 'click', ResetTime);
    AttachEvent(document, 'mousemove', ResetTime);
    AttachEvent(document, 'keypress', ResetTime);
    AttachEvent(window, 'load', ResetTime);
    
    try {
        _localStorage = window.localStorage;
    }
    catch (ex) {
    }
    
    _idleSecondsTimer = window.setInterval(CheckIdleTime, 1000);
    
    function GetLastResetTimeStamp() {
        var lastResetTimeStamp = 0;
        if (_localStorage) {
            lastResetTimeStamp = parseInt(_localStorage[_localStorageKey], 10);
            if (isNaN(lastResetTimeStamp) || lastResetTimeStamp < 0)
                lastResetTimeStamp = (new Date()).getTime();
        } else {
            lastResetTimeStamp = _lastResetTimeStamp;
        }
    
        return lastResetTimeStamp;
    }
    
    function SetLastResetTimeStamp(timeStamp) {
        if (_localStorage) {
            _localStorage[_localStorageKey] = timeStamp;
        } else {
            _lastResetTimeStamp = timeStamp;
        }
    }
    
    function ResetTime() {
        SetLastResetTimeStamp((new Date()).getTime());
    }
    
    function AttachEvent(element, eventName, eventHandler) {
        if (element.addEventListener) {
            element.addEventListener(eventName, eventHandler, false);
            return true;
        } else if (element.attachEvent) {
            element.attachEvent('on' + eventName, eventHandler);
            return true;
        } else {
            //nothing to do, browser too old or non standard anyway
            return false;
        }
    }
    
    function WriteProgress(msg) {
        var oPanel = document.getElementById("SecondsUntilExpire");
        if (oPanel)
             oPanel.innerHTML = msg;
        else if (console)
            console.log(msg);
    }
    
    function CheckIdleTime() {
        var currentTimeStamp = (new Date()).getTime();
        var lastResetTimeStamp = GetLastResetTimeStamp();
        var secondsDiff = Math.floor((currentTimeStamp - lastResetTimeStamp) / 1000);
        if (secondsDiff <= 0) {
            ResetTime();
            secondsDiff = 0;
        }
        WriteProgress((IDLE_TIMEOUT - secondsDiff) + "");
        if (secondsDiff >= IDLE_TIMEOUT) {
            window.clearInterval(_idleSecondsTimer);
            ResetTime();
            alert("Time expired!");
            document.location.href = "logout.html";
        }
    }
    

    The refactored code above is using local storage to keep track of when the counter was last reset, and also reset it on each new tab that is opened which contains the code, then the counter will be the same for all tabs, and resetting in one will result in reset of all tabs. Since Stack Snippets do not allow local storage, it’s pointless to host it there so I’ve made a fiddle:
    http://jsfiddle.net/yahavbr/gpvqa0fj/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the jQuery Stopwatch plugin to track time on a browser based
I know you can track a svn repo with git by using git svn
How can you keep track of time in a simple embedded system, given that
Have I missed any obvious things that you can log to keep track on
How can i track ajax call time out in the following simple ajax function
Is there any simple way of using Google Analytics to track downloads of a
I am using word application host in browser. According to my application I want
I know that Mercurial can track renames of files, but how do I get
Can fogbugz track case dependencies?
How can I track the memory allocations in C++, especially those done by new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.