Skip to content

Instantly share code, notes, and snippets.

@juanmhidalgo
Last active June 15, 2016 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanmhidalgo/2a5d2f3dce461b0a2619c38987ced3e9 to your computer and use it in GitHub Desktop.
Save juanmhidalgo/2a5d2f3dce461b0a2619c38987ced3e9 to your computer and use it in GitHub Desktop.
;(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