
Light onresize dispatcher
By:
cGuille on
Jun 20th, 2012 | syntax:
JavaScript | size: 0.77 KB | hits: 41 | expires: Never
/*
You'll see that there is only one "dispatchResizeEvent" logged for several "onresize" logs.
It means that the code you compute on window resize won't be executed too much times, making your app more responsive if those computings are heavy.
You can impact the dispatch frequency by setting the setTimeout() delay parameter (here 150)
Do not forget to remove the console.log() calls when using this code for real (console.log() is fucking slow).
*/
var timer;
window.onresize = function () {
console.log("onresize");
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(dispatchResizeEvent, 150);
};
function dispatchResizeEvent() {
console.log("dispatchResizeEvent");
// Here the code to process on window resize.
}