1 var setTimeout, 2 clearTimeout, 3 setInterval, 4 clearInterval; 5 6 (function () { 7 var counter = 1; 8 var ids = {}; 9 /** 10 * Executes a function after a specified delay. 11 * The fn is a function to be executed after <code>delay</code> milliseconds. 12 * The function will be executed only once after the specified delay. 13 * 14 * @param {Function} fn the function to be executed at some point in the future. 15 * @param {int} [delay=0] the number of milliseconds (thousandths of a second) 16 * that the function call should be delayed by. Default value for <code>delay</code> is 0 17 * @returns An object representing the task to be executed, which can be used later with <code>clearTimeout</code>. 18 */ 19 setTimeout = function (fn, delay) { 20 var timerDelay = delay || 0; 21 var id = counter++; 22 var args = []; 23 for (var i = 2; i < arguments.length; i++) { 24 args.push(arguments[i]); 25 } 26 var timer = new javax.swing.Timer(timerDelay, new java.awt.event.ActionListener({ 27 'actionPerformed':function() { 28 fn.apply(this, args); 29 } 30 })); 31 timer.setRepeats(false); 32 timer.start(); 33 ids[id] = timer; 34 return id; 35 }; 36 37 /** 38 * Clears the delay set to a function by <code>setTimeout</code>. 39 * @param id - is the ID of the timeout that need to be clear, returned by the <code>setTimeout</code>. 40 */ 41 clearTimeout = function (id) { 42 if(ids.hasOwnProperty(id)) { 43 ids[id].stop(); 44 delete ids[id]; 45 } 46 }; 47 48 /** 49 * Executes a function repeatedly, with fixed delay between each call of the function. 50 * The fn is a function to be executed after <code>delay</code> milliseconds. 51 * The function will be executed repeatedly after the specified delay. 52 * 53 * @param {Function} fn the function to be executed periodically 54 * @param {int} [delay=0] the number of milliseconds (thousandths of a second) 55 * that the function call should be delayed by. Default value for <code>delay</code> is 0 56 * @returns An object representing the task to be executed, which can be used later with <code>clearInterval</code>. 57 */ 58 setInterval = function (fn, delay) { 59 var timerDelay = delay || 0; 60 var id = counter++; 61 var args = []; 62 for (var i = 2; i < arguments.length; i++) { 63 args.push(arguments[i]); 64 } 65 var timer = new javax.swing.Timer(timerDelay, new java.awt.event.ActionListener({ 66 'actionPerformed':function() { 67 fn.apply(this, args); 68 } 69 })); 70 timer.start(); 71 ids[id] = timer; 72 return id; 73 }; 74 /** 75 * Clears the execution delay of a function set by <code>setInterval</code>. 76 * @function 77 * @param id - is the ID of the interval that need to be clear, returned by the <code>setInterval</code>. 78 */ 79 clearInterval = clearTimeout; 80 81 })(); 82 83 /** 84 * var foo = function foo(arg1,arg2){ 85 if(arg1){ 86 console.log("timeout with argument1"+arg1); 87 }else{ 88 console.log("timeout with argument1"+arg1); 89 } 90 if(arg2){ 91 console.log("timeout with argument2"+arg2); 92 }else{ 93 console.log("timeout with argument2"+arg2); 94 } 95 }; 96 97 var timeoutId = setTimeout(foo,3000,true,false); 98 var intervalId = setInterval(foo,5000,false,true,true); 99 clearTimeout(timeoutId); 100 clearInterval(intervalId); 101 */ 102