Don't like ads? PRO users don't see any ads ;-)
Guest

Light onresize dispatcher

By: cGuille on Jun 20th, 2012  |  syntax: JavaScript  |  size: 0.77 KB  |  hits: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.     You'll see that there is only one "dispatchResizeEvent" logged for several "onresize" logs.
  3.     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.
  4.     You can impact the dispatch frequency by setting the setTimeout() delay parameter (here 150)
  5.  
  6.     Do not forget to remove the console.log() calls when using this code for real (console.log() is fucking  slow).
  7. */
  8. var timer;
  9.  
  10. window.onresize = function () {
  11.     console.log("onresize");
  12.     if (timer) {
  13.         clearTimeout(timer);
  14.     }
  15.     timer = setTimeout(dispatchResizeEvent, 150);
  16. };
  17. function dispatchResizeEvent() {
  18.     console.log("dispatchResizeEvent");
  19.     // Here the code to process on window resize.
  20. }