1+ /**
2+ * jquery.wait - insert simple delays into your jquery method chains
3+ *
4+ * jquery.wait allows you to easily insert a delay into a chain of jquery
5+ * methods. This allows you to use timeouts without uglifying your code and
6+ * without having to use a custom queue.
7+ *
8+ * example:
9+ * // add a class to element #foo, then remove 5 seconds later
10+ *
11+ * // without jquery.wait
12+ * $('#foo').addClass('myClass');
13+ * window.setTimeout(function(){
14+ * $('#foo').removeClass('myClass');
15+ * }, 5000);
16+ *
17+ * // with jquery.wait
18+ * $('#foo').addClass('myClass').wait(5000).removeClass('myClass');
19+ *
20+ * jquery.wait will work with any default jquery object methods, as well as any
21+ * methods provided by plugins loaded *before* jquery.wait.
22+ *
23+ * !important - if you are using jquery.wait to add/remove classes that controll
24+ * css transitions, the duration of the wait needs to be slightly longer than
25+ * the transition time. So, if in the example above the class .myClass added a
26+ * 5 second transition of some sort, i would need to make the wait time longer.
27+ * I recommend 100ms longer, though your needs may vary depending on the
28+ * complexity of the animation.
29+ *
30+ * If you are chaining jQuery transitions, it is better to use the default
31+ * jquery .delay method, which has the same syntax but works with jquery queues
32+ *
33+ * @author Matthew Lee [email protected] 34+ */
35+
36+ ( function ( $ ) {
37+ function jQueryDummy ( $real , delay , _fncQueue ) {
38+ var dummy = this ;
39+ this . fncQueue = ( ! ! _fncQueue ) ? _fncQueue : [ ] ;
40+ this . delayCompleted = false ;
41+ this . real = $real ;
42+
43+ this . timeoutKey = window . setTimeout ( function ( ) {
44+ dummy . performDummyQueueActions . call ( dummy ) ;
45+ } , delay ) ;
46+ }
47+
48+ jQueryDummy . prototype . addToQueue = function ( fnc , arg ) {
49+ if ( ! this . delayCompleted || this . fncQueue . length > 0 ) {
50+ this . fncQueue . unshift ( { fnc : fnc , arg : arg } ) ;
51+ }
52+ else {
53+ this . real [ fnc ] . apply ( this . real , arg ) ;
54+ }
55+ return this ;
56+ } ;
57+
58+ jQueryDummy . prototype . performDummyQueueActions = function ( ) {
59+ this . delayCompleted = true ;
60+
61+ var next ;
62+ while ( this . fncQueue . length > 0 ) {
63+ next = this . fncQueue . pop ( ) ;
64+ // console.log('performing delayed function '+next.fnc+'('+next.arg+')');
65+ if ( next . fnc === 'wait' ) {
66+ next . arg . push ( this . fncQueue ) ;
67+ this . real [ next . fnc ] . apply ( this . real , next . arg ) ;
68+ break ;
69+ } else {
70+ this . real [ next . fnc ] . apply ( this . real , next . arg ) ;
71+ }
72+ }
73+ } ;
74+
75+
76+ $ . fn . wait = function ( delay , _queue ) {
77+ return new jQueryDummy ( this , delay , _queue ) ;
78+ } ;
79+
80+ for ( var fnc in $ . fn ) {
81+ jQueryDummy . prototype [ fnc ] = ( function ( fnc ) {
82+
83+ return function ( ) {
84+ var arg = Array . prototype . slice . call ( arguments ) ;
85+ return this . addToQueue ( fnc , arg ) ;
86+ } ;
87+
88+ } ) ( fnc ) ;
89+ }
90+
91+ } ) ( jQuery ) ;
0 commit comments