×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Jesse Tinell
Added: Aug 21, 2013 2:48 PM
Views: 41
With a debouncer a function within the resize() is prevented to run on every single resize-event.
In other words: when the window has not been resized for 200ms it will run the function.
  1. $(window).resize(debouncer(function (e) {
  2.     //Do stuff
  3. }));
  4.  
  5. function debouncer(func, timeout) {
  6.     var timeoutID, timeout = timeout || 200;
  7.     return function () {
  8.         var scope = this, args = arguments;
  9.         clearTimeout(timeoutID);
  10.         timeoutID = setTimeout(function () {
  11.             func.apply(scope, Array.prototype.slice.call(args));
  12.         }, timeout);
  13.     }
  14. }
  15.  
  16.