Most modern languages have the concept of letting your code sleep for a specified period of time. This is useful when you want to perform a particular task at some point down the road. It's usually implemented such that your program requests to yield control for a certain number of milliseconds. The OS will usually guarantee that your code will remain in blissful slumber for at least that amount of time, plus whatever random delay is imposed by multitasking and OS overhead.

Some folks use sleep intervals to allow for periodic updating of some tally, or perhaps a clock. Of course, smart programmers check the actual time after being awakened so as not to allow the difference between requested and actual sleep time to skew things.

One particular developer thought it'd be a good idea to have a countdown timer to auto-logoff the session. To this end, they used a timer to sleep for one second, then used string manipulation to convert the timer's text for display, and repeated until the auto-logoff interval had expired. Of course, the method gets triggered every time the page is updated in any way; even if it's an AJAX partial post-back. As such, every time the panel is refreshed, the timer is decremented, effectively changing the rate at which time passes:

//Logout Clock Function
  function runLogoutClock() {
    var prevValue = document.getElementById('ctl00_lblTimer').innerHTML;
    var newValue = prevValue;
    var arr = prevValue.split(':');

    if (prevValue != "00:00") {
        if (parseInt(arr[1], 10) == 0) {
            if ((parseInt(arr[0], 10) - 1).toString().length == 1) {
                newValue = '0' + (parseInt(arr[0], 10) - 1).toString() + ':' + '59';
            }
            else {
                newValue = (parseInt(arr[0], 10) - 1).toString() + ':' + '59';
            }
        }
        else {
            if ((parseInt(arr[1], 10) - 1).toString().length == 1) {
                newValue = arr[0].toString() + ':' + '0' + (parseInt(arr[1], 10) - 1).toString();
            }
            else {
                newValue = arr[0].toString() + ':' + (parseInt(arr[1], 10) - 1).toString();
            }
        }
    }

    document.getElementById('ctl00_lblTimer').innerHTML = newValue;

    if (newValue == "00:00") {
        document.getElementById('ctl00_lblWaitClock').innerHTML = "10:01";
        runWaitForClock();
        var modal = $find('ctl00_mpeLogout');
        modal.show();
    }
    else {
        theTime = window.setTimeout("runLogoutClock()", 1000);
    }
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!