Skip to content

Commit d7160eb

Browse files
author
John Beppu
committed
$.ev
1 parent 858be7f commit d7160eb

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

jquery.ev.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
(function($){
2+
3+
$.ev = $.ev || {
4+
5+
handlers : {},
6+
running : false,
7+
xhr : null,
8+
9+
log: function() {
10+
if (console) console.log(arguments);
11+
},
12+
13+
run: function(events) {
14+
var i;
15+
for (i = 0; i < events.length; i++) {
16+
var e = events[i];
17+
if (!e) continue;
18+
var h = this.handlers[e.type];
19+
if (h) h(e);
20+
}
21+
},
22+
23+
stop: function() {
24+
this.running = false;
25+
if (this.xhr) {
26+
this.xhr.abort();
27+
this.xhr = null;
28+
}
29+
// Maybe it should let the server side know that it stopped listening.
30+
},
31+
32+
loop: function(url, channels) {
33+
var self = this;
34+
this.running = true;
35+
if (!channels) channels = [];
36+
this.xhr = $.ajax({
37+
type : 'GET',
38+
dataType : 'json',
39+
url : url,
40+
data : { channels: channels },
41+
success : function(events, status) {
42+
self.log('success', events);
43+
self.run(events)
44+
},
45+
complete : function(xhr, status) {
46+
var delay;
47+
if (status == 'success') {
48+
delay = 100;
49+
} else {
50+
self.log('status: ' + status, '; waiting before long-polling again...');
51+
delay = 5000;
52+
}
53+
window.setTimeout(function(){
54+
if (self.running) self.loop(url, channels);
55+
}, delay);
56+
}
57+
});
58+
}
59+
60+
};
61+
62+
})(jQuery);

0 commit comments

Comments
 (0)