Wait Timer

This will execute a single callback after a series of events are done executing. For example, execute something once 1000ms after the window is done resizing. Or execute something 500ms after a series of keyup event is done.

by mrbinky3000

HTML

<p>resize the page to see the events triggered</p>

JavaScript

var Wait = function() {
    "use strict";
    var t, _d = function(callback, ms) {
        if (typeof t === 'number') {
            clearTimeout(t);
        }
        t = setTimeout(callback, ms);
    };
    return {
        'delay': function(callback, ms) {
            _d(callback,ms);            
        }
    };
};


$(document).ready(function() {
    "use strict";
    var bob = Wait();
    var sam = Wait();
    $(window).on('resize', function() {
        bob.delay(function() {
            alert('hello 1');
        }, 1000);
        sam.delay(function() {
            alert('hello 2');
        }, 1000);
    });
});