Skip to content

Instantly share code, notes, and snippets.

@tylerthebuildor
Created July 29, 2015 22:39
Show Gist options
  • Save tylerthebuildor/c874995c82258d3f5cb9 to your computer and use it in GitHub Desktop.
Save tylerthebuildor/c874995c82258d3f5cb9 to your computer and use it in GitHub Desktop.
The JavaScript scroll event isn't very peformant this MDN snippet fixes that with a modified event.
(function() {
'use strict';
var throttle = function(type, name, obj) {
obj = obj || window;
var running = false;
var func = function() {
if (running) { return; }
running = true;
obj.requestAnimationFrame(function() {
obj.dispatchEvent(new CustomEvent(name));
running = false;
});
};
obj.addEventListener(type, func);
};
/* init - you can init any event */
throttle ('scroll', 'optimizedScroll');
})();
// handle event
window.addEventListener('optimizedScroll', function() {
'use strict';
console.log('Resource conscious scroll callback!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment