1 dump('entering util/exec.js\n');
3 if (typeof util == 'undefined') var util = {};
4 util.exec = function(chunk_size) {
7 this.chunk_size = chunk_size || 1;
8 obj.clear_timer = function() {
10 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: Clearing interval with id = ' + obj._intervalId + '\n'); }
11 window.clearInterval(obj._intervalId);
13 alert('Error in clear_timer: ' + E);
20 util.exec.prototype = {
21 // This will create a timer that polls the specified array and shifts off functions to execute
22 'timer' : function(funcs,interval) {
26 var intervalId = window.setInterval(
28 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: ' + location.pathname + ': Running interval with id = ' + intervalId + '\n'); }
29 var i = obj.chunk_size;
30 while (funcs.length > 0 && i > 0) {
36 obj._intervalId = intervalId;
37 window.addEventListener('unload',obj.clear_timer,false);
40 'clear_timer' : function() {
42 if (obj._intervalId) {
44 window.removeEventListener('unload',obj.clear_timer,false);
47 // This executes a series of functions, but tries to give other events/functions a chance to
48 // execute between each one.
49 'chain' : function () {
52 for (var i = 0; i < arguments.length; i++) {
53 var arg = arguments[i];
54 switch(arg.constructor.name) {
59 for (var j = 0; j < arg.length; j++) {
60 if (typeof arg[j] == 'function') args.push( arg[j] );
65 if (typeof arg[j] == 'function') args.push( arg[j] );
70 if (args.length > 0) setTimeout(
73 for (var i = 0; (i < args.length && i < obj.chunk_size) ; i++) {
75 if (typeof args[i] == 'function') {
76 dump('EXEC: executing queued function. intervalId = ' + obj._intervalId + '\n');
78 dump('EXEC: function = ' + args[i] + '\n');
82 alert('FIXME -- typeof args['+i+'] == ' + typeof args[i]);
85 dump('EXEC: util.exec.chain error: ' + js2JSON(E) + '\n');
86 var keep_going = false;
87 if (typeof obj.on_error == 'function') {
88 keep_going = obj.on_error(E);
91 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: chain not broken\n'); }
93 if (args.length > 1 ) obj.chain( args.slice(1) );
96 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: another error: ' + js2JSON(E) + '\n'); }
99 if (typeof obj.debug != 'undefined' && obj.debug) { dump('EXEC: chain broken\n'); }
104 if (args.length > obj.chunk_size ) obj.chain( args.slice(obj.chunk_size) );
113 dump('exiting util/exec.js\n');