( function( $ ) { "use strict"; // Define default settings var defaults = { action: function() {}, runOnLoad: false, duration: 500 }; // Define global variables var settings = defaults, running = false, start; var methods = {}; // Initial plugin configuration methods.init = function() { // Allocate passed arguments to settings based on type for( var i = 0; i <= arguments.length; i++ ) { var arg = arguments[i]; switch ( typeof arg ) { case "function": settings.action = arg; break; case "boolean": settings.runOnLoad = arg; break; case "number": settings.duration = arg; break; } } // Process each matching jQuery object return this.each(function() { if( settings.runOnLoad ) { settings.action(); } $(this).resize( function() { methods.timedAction.call( this ); } ); } ); }; methods.timedAction = function( code, millisec ) { var doAction = function() { var remaining = settings.duration; if( running ) { var elapse = new Date() - start; remaining = settings.duration - elapse; if( remaining <= 0 ) { // Clear timeout and reset running variable clearTimeout(running); running = false; // Perform user defined function settings.action(); return; } } wait( remaining ); }; var wait = function( time ) { running = setTimeout( doAction, time ); }; // Define new action starting time start = new Date(); // Define runtime settings if function is run directly if( typeof millisec === 'number' ) { settings.duration = millisec; } if( typeof code === 'function' ) { settings.action = code; } // Only run timed loop if not already running if( !running ) { doAction(); } }; $.fn.afterResize = function( method ) { if( methods[method] ) { return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ) ); } else { return methods.init.apply( this, arguments ); } }; })(jQuery);