Created
September 28, 2015 20:14
-
-
Save arturparkhisenko/f933e96aeadf2647713e to your computer and use it in GitHub Desktop.
resize and throttle with rAF
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
//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"); | |
})(); | |
// 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