holderdeord/hdo-site

View on GitHub
app/assets/javascripts/throttler.js

Summary

Maintainability
A
0 mins
Test Coverage
/*global HDO */

(function (H) {
    H.throttler = {
        create: function (timeout) {
            var instance = Object.create(this);
            instance.timeout = timeout;
            return instance;
        },

        queue: function (query, callback) {
            var self = this;
            clearTimeout(this.timer);

            this.timer = setTimeout(function () {
                delete self.timer;
                if (query !== self.lastQuery) {
                    callback(query);
                }
                self.lastQuery = query;
            }, self.timeout);
        }
    };
}(HDO));