-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatter.js
More file actions
41 lines (39 loc) · 1.13 KB
/
chatter.js
File metadata and controls
41 lines (39 loc) · 1.13 KB
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
(function() {
function append_messages(msgs) {
for (var i=0; i<msgs.length; i++) {
var msg = msgs[i];
$('#messages').append(
$('<li>').text(msg.message)
);
}
// Return the id of the last message, increased by one
return msgs[msgs.length-1].id + 1;
}
function poll_messages() {
var statediv = $('#chat-state');
if (statediv.data('running') === true) {
return;
} else {
statediv.data('running', true);
}
var id = statediv.data('id');
if (!id) id = 0;
$.getJSON(
'/wait',
{'id': id},
function(data, status) {
statediv.data('running', false);
var last_id = append_messages(data);
statediv.data('id', last_id);
}
);
}
$(document).ready(
function() {
poll_messages();
var interval = setInterval(
function() {
poll_messages();
}, 5000);
});
})();