Skip to content

Instantly share code, notes, and snippets.

Last active Jun 22, 2018
What would you like to do?
JavaScript Throttle using requestAnimationFrame
function throttle(type, name, obj) {
var running = false;
obj = obj || window;
var func = function() {
if (running) return;
running = true;
requestAnimationFrame(function() {
obj.dispatchEvent(new CustomEvent(name));
running = false;
obj.addEventListener(type, func);
// Usage...
// Throttle scroll event
throttle('scroll', 'scroll.throttled');
// Listen to it
window.addEventListener('scroll.throttled', function() {
console.log('Event fired');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment