Skip to content

Instantly share code, notes, and snippets.

@mika76
Created May 21, 2019 11:27
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 mika76/d076fedb01248b70d90809a3a099971a to your computer and use it in GitHub Desktop.
Save mika76/d076fedb01248b70d90809a3a099971a to your computer and use it in GitHub Desktop.
resize event: requestAnimationFrame + customEvent
(function() {
var throttle = function(type, name, obj) {
obj = obj || window;
var running = false;
var func = function() {
if (running) { return; }
running = true;
requestAnimationFrame(function() {
obj.dispatchEvent(new CustomEvent(name));
running = false;
});
};
obj.addEventListener(type, func);
};
/* init - you can init any event */
throttle("resize", "optimizedResize");
})();
// handle event
window.addEventListener("optimizedResize", function() {
console.log("Resource conscious resize callback!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment