11/*! Copyright (c) 2014 - Paul Tavares - purtuga - @paul_tavares - MIT License */
22; ( function ( $ ) {
3-
3+
44 /**
55 * Delays the execution of a function until an expression returns true.
6- * The expression is checked every 100 milliseconds for as many tries
6+ * The expression is checked every 100 milliseconds for as many tries
77 * as defined in in the attempts option
8- *
8+ *
99 * @param {Object } options
1010 * @param {Function } options.when
1111 * Function to execute on every interval.
2121 * a failure.
2222 * @param {Interger } [options.delayed=0]
2323 * Number of miliseconds to wait before execution
24- is started. Default is imediately.
25- *
24+ is started. Default is imediately.
25+ *
2626 * @return {jQuery.Promise }
27- *
28- * @example
29- *
27+ *
28+ * @example
29+ *
3030 * $.doWhen({
3131 * when: function(){
3232 * return false;
4040 * })
4141 * .then(function(){
4242 * alert("resolved.");
43- * });
44- *
43+ * });
44+ *
4545 */
4646 $ . doWhen = function ( options ) {
47-
47+
4848 return $ . Deferred ( function ( dfd ) {
49-
49+
5050 var opt = $ . extend ( { } , {
5151 when : null ,
5252 exec : function ( ) { } ,
5959 checkId : null
6060 } ) ,
6161 startChecking = function ( ) {
62-
62+
6363 // Check condition now and if true, then resolve object
6464 if ( opt . when ( ) === true ) {
65-
65+
6666 opt . exec . call ( dfd . promise ( ) ) ;
6767 dfd . resolve ( ) ;
6868 return ;
69-
69+
7070 }
71-
71+
7272 // apply minimal UI and hide the overlay
7373 opt . checkId = setInterval ( function ( ) {
74-
74+
7575 if ( opt . attempts === 0 ) {
76-
76+
7777 clearInterval ( opt . checkId ) ;
7878 dfd . reject ( ) ;
79-
79+
8080 } else {
81-
81+
8282 -- opt . attempts ;
83-
83+
8484 if ( opt . when ( ) === true ) {
85-
85+
8686 opt . attempts = 0 ;
8787 clearInterval ( opt . checkId ) ;
8888 opt . exec . call ( dfd . promise ( ) ) ;
8989 dfd . resolve ( ) ;
90-
90+
9191 }
92-
92+
9393 }
94-
94+
9595 } , opt . interval ) ;
96-
96+
9797 } ;
98-
98+
9999 if ( opt . delayed > 0 ) {
100-
100+
101101 setTimeout ( startChecking , Number ( opt . delayed ) ) ;
102-
102+
103103 } else {
104-
104+
105105 startChecking ( ) ;
106-
106+
107107 }
108-
108+
109109 } ) . promise ( ) ;
110-
111- } ;
110+
111+ } ;
112112
113113} ) ( jQuery ) ;
0 commit comments