source: main/trunk/model-interfaces-dev/opotiki/scripts/jquery.timers.1.2.js@ 34087

Last change on this file since 34087 was 34087, checked in by ak19, 4 years ago

Cher (Chai Lin): Adding the Opotiki interface.

File size: 3.3 KB
Line 
1/**
2 * jQuery.timers - Timer abstractions for jQuery
3 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
4 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
5 * Date: 2009/10/16
6 *
7 * @author Blair Mitchelmore
8 * @version 1.2
9 *
10 **/
11
12jQuery.fn.extend({
13 everyTime: function(interval, label, fn, times) {
14 return this.each(function() {
15 jQuery.timer.add(this, interval, label, fn, times);
16 });
17 },
18 oneTime: function(interval, label, fn) {
19 return this.each(function() {
20 jQuery.timer.add(this, interval, label, fn, 1);
21 });
22 },
23 stopTime: function(label, fn) {
24 return this.each(function() {
25 jQuery.timer.remove(this, label, fn);
26 });
27 }
28});
29
30jQuery.extend({
31 timer: {
32 global: [],
33 guid: 1,
34 dataKey: "jQuery.timer",
35 regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
36 powers: {
37 // Yeah this is major overkill...
38 'ms': 1,
39 'cs': 10,
40 'ds': 100,
41 's': 1000,
42 'das': 10000,
43 'hs': 100000,
44 'ks': 1000000
45 },
46 timeParse: function(value) {
47 if (value == undefined || value == null)
48 return null;
49 var result = this.regex.exec(jQuery.trim(value.toString()));
50 if (result[2]) {
51 var num = parseFloat(result[1]);
52 var mult = this.powers[result[2]] || 1;
53 return num * mult;
54 } else {
55 return value;
56 }
57 },
58 add: function(element, interval, label, fn, times) {
59 var counter = 0;
60
61 if (jQuery.isFunction(label)) {
62 if (!times)
63 times = fn;
64 fn = label;
65 label = interval;
66 }
67
68 interval = jQuery.timer.timeParse(interval);
69
70 if (typeof interval != 'number' || isNaN(interval) || interval < 0)
71 return;
72
73 if (typeof times != 'number' || isNaN(times) || times < 0)
74 times = 0;
75
76 times = times || 0;
77
78 var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
79
80 if (!timers[label])
81 timers[label] = {};
82
83 fn.timerID = fn.timerID || this.guid++;
84
85 var handler = function() {
86 if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
87 jQuery.timer.remove(element, label, fn);
88 };
89
90 handler.timerID = fn.timerID;
91
92 if (!timers[label][fn.timerID])
93 timers[label][fn.timerID] = window.setInterval(handler,interval);
94
95 this.global.push( element );
96
97 },
98 remove: function(element, label, fn) {
99 var timers = jQuery.data(element, this.dataKey), ret;
100
101 if ( timers ) {
102
103 if (!label) {
104 for ( label in timers )
105 this.remove(element, label, fn);
106 } else if ( timers[label] ) {
107 if ( fn ) {
108 if ( fn.timerID ) {
109 window.clearInterval(timers[label][fn.timerID]);
110 delete timers[label][fn.timerID];
111 }
112 } else {
113 for ( var fn in timers[label] ) {
114 window.clearInterval(timers[label][fn]);
115 delete timers[label][fn];
116 }
117 }
118
119 for ( ret in timers[label] ) break;
120 if ( !ret ) {
121 ret = null;
122 delete timers[label];
123 }
124 }
125
126 for ( ret in timers ) break;
127 if ( !ret )
128 jQuery.removeData(element, this.dataKey);
129 }
130 }
131 }
132});
133
134jQuery(window).bind("unload", function() {
135 jQuery.each(jQuery.timer.global, function(index, item) {
136 jQuery.timer.remove(item);
137 });
138});
Note: See TracBrowser for help on using the repository browser.