setTimeout Example could be simplified and show more info

UNCONFIRMED
Unassigned

Status

P5
normal
UNCONFIRMED
2 years ago
2 years ago

People

(Reporter: mvoss9000, Unassigned)

Tracking

Details

(URL)

(Reporter)

Description

2 years ago
:: Developer Documentation Request

      Request Type: New Documentation
     Gecko Version: unspecified
 Technical Contact: 

:: Details

A suggestion.  The alert object, setting 'this.timeoutID = undefined', .bind(this) aren't needed for the example.  Just four lines show the clearTimeout behavior:

    var timeoutID;
    window.onclick = function() {
        window.clearTimeout(timeoutID);
        timeoutID = window.setTimeout(function() { alert('Wake Up!') }, 1000);
    }

Or with ECMA6:

    var timeoutID;
    window.onclick = function() {
        window.clearTimeout(timeoutID);
        timeoutID = window.setTimeout(() => alert('Wake Up!'), 1000);
    }

Or add a bit back to this stripped down version to show the click/timeout behavior more clearly:

    var timeoutID;
    var clickCount = 0;
    var alertCount = 0;
    window.onclick = function() {
        clickCount++;
        window.clearTimeout(timeoutID);
        timeoutID = window.setTimeout(function() {
            alertCount++;
            alert('alert ' + alertCount + ' (' + clickCount + ' clicks)');
        }, 1000);
    }
(Reporter)

Comment 1

2 years ago
That last example is what made the behavior most clear to me, by the way because it shows how the clicks are registered separately from the alerts, and by commenting out 'window.clearTimeout()', the behavior changes for fast clicks showing 'alert 1 (4 clicks)' 'alert 2 (4 clicks)' 'alert 3 (4 clicks)' ...
Component: JavaScript → API: DOM
You need to log in before you can comment on or make changes to this bug.