JAVASCRIPT   38

debouncedresize.js

Guest on 12th August 2021 06:52:37 PM

  1. /*
  2.  * debouncedresize: special jQuery event that happens once after a window resize
  3.  *
  4.  * latest version and complete README available on Github:
  5.  * https://github.com/louisremi/jquery-smartresize
  6.  *
  7.  * Copyright 2012 @louis_remi
  8.  * Licensed under the MIT license.
  9.  */
  10. (function($) {
  11.     "use strict";
  12.     var $event = $.event,
  13.         $special,
  14.         resizeTimeout;
  15.    
  16.     $special = $event.special.debouncedresize = {
  17.         setup: function() {
  18.             $( this ).on( "resize", $special.handler );
  19.         },
  20.         teardown: function() {
  21.             $( this ).off( "resize", $special.handler );
  22.         },
  23.         handler: function( event, execAsap ) {
  24.             // Save the context
  25.             var context = this,
  26.                 args = arguments,
  27.                 dispatch = function() {
  28.                     // set correct event type
  29.                     event.type = "debouncedresize";
  30.                     $event.dispatch.apply( context, args );
  31.                 };
  32.    
  33.             if ( resizeTimeout ) {
  34.                 clearTimeout( resizeTimeout );
  35.             }
  36.    
  37.             execAsap ?
  38.                 dispatch() :
  39.                 resizeTimeout = setTimeout( dispatch, $special.threshold );
  40.         },
  41.         threshold: 150
  42.     };
  43.    
  44. })(jQuery);

Raw Paste


Login or Register to edit or fork this paste. It's free.