Skip to content

Instantly share code, notes, and snippets.

@juanmhidalgo
Last active Jun 15, 2016
Embed
What would you like to do?
;(function() {
window.throttle = function (type, name, obj) {
obj = obj || window;
var running = false;
var func = function () {
if (running) { return; }
running = true;
requestAnimationFrame(function () {
var event;
try {
event = new CustomEvent(name)
} catch(e) {
event = document.createEvent('Event');
event.initEvent(name, true, true);
}
obj.dispatchEvent(event);
running = false;
});
};
obj.addEventListener(type, func);
};
// wrap resize in throttle
throttle('resize', 'optimizedResize');
throttle('scroll', 'optimizedScroll');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment