Closed Bug 1299898 Opened 8 years ago Closed 4 years ago

setTimeout Example could be simplified and show more info

Categories

(Developer Documentation Graveyard :: API: DOM, defect, P5)

All
Other
defect

Tracking

(Not tracked)

RESOLVED WONTFIX

People

(Reporter: mvoss9000, Unassigned)

References

()

Details

:: 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);
    }
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
MDN Web Docs' bug reporting has now moved to GitHub. From now on, please file content bugs at https://github.com/mdn/sprints/issues/ and platform bugs at https://github.com/mdn/kuma/issues/.
Status: UNCONFIRMED → RESOLVED
Closed: 4 years ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.