]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - resources/jquery/jquery.async.js
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / resources / jquery / jquery.async.js
1 /*
2  * jQuery Asynchronous Plugin 1.0
3  *
4  * Copyright (c) 2008 Vincent Robert (genezys.net)
5  * Dual licensed under the MIT (MIT-LICENSE.txt)
6  * and GPL (GPL-LICENSE.txt) licenses.
7  *
8  */
9 (function($){
10
11 // opts.delay : (default 10) delay between async call in ms
12 // opts.bulk : (default 500) delay during which the loop can continue synchronously without yielding the CPU
13 // opts.test : (default true) function to test in the while test part
14 // opts.loop : (default empty) function to call in the while loop part
15 // opts.end : (default empty) function to call at the end of the while loop
16 $.whileAsync = function(opts)
17 {
18         var delay = Math.abs(opts.delay) || 10,
19                 bulk = isNaN(opts.bulk) ? 500 : Math.abs(opts.bulk),
20                 test = opts.test || function(){ return true; },
21                 loop = opts.loop || function(){},
22                 end  = opts.end  || function(){};
23         
24         (function(){
25
26                 var t = false,
27                         begin = new Date();
28                         
29                 while( t = test() )
30                 {
31                         loop();
32                         if( bulk === 0 || (new Date() - begin) > bulk )
33                         {
34                                 break;
35                         }
36                 }
37                 if( t )
38                 {
39                         setTimeout(arguments.callee, delay);
40                 }
41                 else
42                 {
43                         end();
44                 }
45                 
46         })();
47 };
48
49 // opts.delay : (default 10) delay between async call in ms
50 // opts.bulk : (default 500) delay during which the loop can continue synchronously without yielding the CPU
51 // opts.loop : (default empty) function to call in the each loop part, signature: function(index, value) this = value
52 // opts.end : (default empty) function to call at the end of the each loop
53 $.eachAsync = function(array, opts)
54 {
55         var i = 0,
56                 l = array.length,
57                 loop = opts.loop || function(){};
58         
59         $.whileAsync(
60                 $.extend(opts, {
61                         test: function(){ return i < l; },
62                         loop: function()
63                         {
64                                 var val = array[i];
65                                 return loop.call(val, i++, val);
66                         }
67                 })
68         );
69 };
70
71 $.fn.eachAsync = function(opts)
72 {
73         $.eachAsync(this, opts);
74         return this;
75 }
76
77 })(jQuery);
78