Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Forked from arturparkhisenko/resize.js
Last active September 14, 2017 12:26
Show Gist options
  • Save andreasvirkus/9b1d606919e1258e04df7582ac4f3623 to your computer and use it in GitHub Desktop.
Save andreasvirkus/9b1d606919e1258e04df7582ac4f3623 to your computer and use it in GitHub Desktop.
resize and throttle with rAF
//https://developer.mozilla.org/en-US/docs/Web/Events/resize
(function() {
var throttle = function(type, name, obj_) {
var 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");
})();
// usage
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