-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.poller.js
75 lines (64 loc) · 1.95 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
63
64
65
66
67
68
69
70
71
72
73
74
75
function JqueryPoller(arrcheck){
var self = this;
self.curr_arrcheck = arrcheck;
self.full_arrcheck = arrcheck;
self.wait = 1000;
self.cont = true;
self.ajaxopt = {
dataType: 'json',
success: function(data, textStatus){
console.log(data);
new_arrcheck = {}
for(icode in data.codes){
if(data.codes[icode]=='ok'){
// ok, go callback
eval(self.full_arrcheck[icode].success+'(icode)');
}else if(data.codes[icode]=='error'){
// error
eval(self.full_arrcheck[icode].error+'(icode)');
}else{
// still in progress
console.log(icode + ' <- ' + self.full_arrcheck[icode],'qqqq');
new_arrcheck[icode] = self.full_arrcheck[icode];
}
}
//self.cont = false;
self.curr_arrcheck = new_arrcheck;
},
error: function(xhr, textStatus, errorThrown){
var txtstat = xhr.statusText;
if(xhr.status != 200){
if(xhr.status == 401){ txtstat = 'devi rieseguire il login.'; }
else if(xhr.status == 404){ txtstat = 'la pagina richiesta non è stata trovata.'; }
else if(xhr.status == 500){ txtstat = 'errore interno, contatta l\'assistenza fresh.'; }
self.cont = false;
alert('Attenzione: '+txtstat);
//location.href='login';
}
}
};
self.addpolling = function(arrcheck){
var full_arrcheck_new = self.full_arrcheck;
var curr_arrcheck_new = self.curr_arrcheck;
for(key in arrcheck){
full_arrcheck_new[key] = arrcheck[key];
curr_arrcheck_new[key] = arrcheck[key];
}
self.full_arrcheck = full_arrcheck_new;
self.curr_arrcheck = curr_arrcheck_new;
if(!self.cont){
self.cont = true;
setTimeout(polling, self.wait);
}
}
var polling = function(){
var countp = countkeys(self.curr_arrcheck);
if(countp<1) self.cont = false;
if(self.cont){
self.ajaxopt.url = 'server_test.php'+'?'+'arr[]=' + arrkeys(self.curr_arrcheck).join('&arr[]=');
$.ajax(self.ajaxopt);
setTimeout(polling, self.wait);
}
}
setTimeout(polling, self.wait);
}