-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjquery.poller.js
62 lines (51 loc) · 1.31 KB
/
jquery.poller.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
// Panmind Cayugan - (C) 2009 Mind2Mind S.r.l.
//
var Poller = function (options) {
var form = $(options.element).find ('form');
var url = form.attr ('action');
var type = options.type || 'POST';
var dataType = options.dataType || null;
var dataFunc = options.dataFunc || function () { return form.serialize (); }
var xhr = null;
var poll = function () {
var self = this;
xhr = $.ajax ({
url : url,
type: type,
data: dataFunc.apply (this, [form]),
dataType : dataType,
beforeSend: options.loading,
complete : options.complete,
success : options.success,
error : options.error
});
};
var id = undefined;
this.start = function () {
if (id)
return; // Already running
if (!options.delayed) {
this.log ('polling');
poll.apply (this);
} else {
this.log ('delaying by ' + options.timeout + 'ms');
}
id = window.setInterval (poll, options.timeout);
};
this.stop = function () {
if (xhr)
xhr.abort ();
if (id) {
this.log ('stopping');
window.clearInterval (id);
id = undefined;
}
};
this.status = function () {
return id ? 'running' : 'stopped';
};
this.log = function (message) {
$.log ('Poller "' + url + '": ' + message);
};
return this;
};