This repository has been archived by the owner on Nov 24, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
afterresize.js
99 lines (74 loc) · 2 KB
/
afterresize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
( 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);