Last active
June 15, 2016 20:08
-
-
Save juanmhidalgo/2a5d2f3dce461b0a2619c38987ced3e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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