-
Notifications
You must be signed in to change notification settings - Fork 0
/
interruptibleInterval.js
122 lines (100 loc) · 4.83 KB
/
interruptibleInterval.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(function( $ ) {
var timers = {};
var settings = {};
var methods = {
set : function(timerId, options) {
if ( timerId in timers ) {
$.error( 'The timerId "' + timerId + '" already exists in interruptibleInterval.timers' );
} else {
var S = settings[timerId] = $.extend({
interruptInterval: 10000
}, options);
if ( !('callback' in S) ) {
$.error( 'Missing required option "callback" in interruptibleInterval' );
}
if ( !('interval' in S) ) {
$.error( 'Missing required option "interval" in interruptibleInterval' );
}
timers[timerId] = window.setInterval( S.callback, S.interval );
if ( 'interrupters' in S ) {
// bind events that interrupt the interval
// The user can provide a collection of jQuery objects, events, and interruptIntervals
for ( interrupter in S.interrupters ) {
var i = S.interrupters[interrupter];
var obj = i.obj;
var e = i.e;
if ( !(obj instanceof jQuery) ) {
$.error( 'The interrupter "' + obj + '" is not a jQuery object' );
} else {
// If no interrupt interval is specified for an individual object,
// the global interruptInterval is used
var interruptInterval;
if ( 'interruptInterval' in i ) {
interruptInterval = i.interruptInterval;
} else {
interruptInterval = S.interruptInterval;
}
// deal with namespacing potentially multiple events (space-separated in e)
e = e.split(' ')
// append namespace to each event
// (use timerId in namespace so we can unbind each timer individually
.map(function(e) { return e+'.interruptibleInterval-'+timerId})
// join 'em back up again
.join(' ');
obj.bind(e, { tId: timerId, ii: interruptInterval },
function(event) {
methods.interrupt(event.data.tId, event.data.ii);
});
}
}
}
}
},
interrupt : function(timerId, interruptInterval) {
if ( !(timerId in timers) ) {
$.error( 'Unknown timerId "' + timerId + '"' );
return;
}
var S = settings[timerId];
// stop the current interval timer
window.clearInterval(timers[timerId]);
// wait for the interruptInterval time before starting the interval timer again
timers[timerId] = window.setInterval(function() {
window.clearInterval(timers[timerId]);
timers[timerId] = window.setInterval( S.callback, S.interval );
},
interruptInterval);
},
clear : function (timerId) {
if ( !(timerId in timers) ) {
$.error( 'Unknown timerId "' + timerId + '"' );
return;
}
window.clearInterval(timers[timerId]);
delete timers[timerId];
var S = settings[timerId];
if ( 'interrupters' in S ) {
// unbind the interval interruption events
for ( interrupter in S.interrupters ) {
var i = S.interrupters[interrupter];
var obj = i.obj;
if ( !(obj instanceof jQuery) ) {
$.error( 'The interrupter "' + obj + '" is not a jQuery object' );
} else {
obj.unbind('.interruptibleInterval-'+timerId);
}
}
}
delete settings[timerId];
}
};
$.fn.interruptibleInterval = function (method) {
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( !method ) {
$.error( 'You must specify an interruptibleInterval method (set, interrupt, or clear)' );
} else {
$.error( 'Method "' + method + '" does not exist on jQuery.interruptibleInterval' );
}
};
})( jQuery );