-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
104 lines (92 loc) · 3.42 KB
/
test.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
function randInt(lowrange, highrange) {
return Math.floor((Math.random() + lowrange) * (highrange + 1))
}
/*
* Basic function to shuffle an array.
*
*/
function shuffle(array) {
for (var i=array.length - 1; i > 0; i--) {
var j = randInt(0, i)
var temp = array[i]
array[i] = array[j]
array[j] = temp
}
}
/*
* Perform set subtraction between two arrays
*/
function setSubtraction(array1, array2) {
var result = new Array();
// find all items in array1 not in array2
for (var i=0; i<array1.length; i++) {
var found = false;
for (var j=0; j<array2.length; j++) {
if (array1[i] === array2[j]) {
found = true;
break;
}
}
if (!found) {
result.push(array[i]);
}
}
return result;
}
var ExtraLifePoller = function () {
this.defaultId = 200590
this.participantId = parseInt(window.location.href.sub(/^.*participantID=(\d+).*/, '$1'))
if (!this.participantId) this.participantId = this.defaultId
this.participantUrl = 'http://www.extra-life.org/index.cfm?fuseaction=donorDrive.participant&format=json&participantID=' + this.participantId
this.donationsUrl = 'http://www.extra-life.org/index.cfm?fuseaction=donorDrive.participantDonations&format=json&participantID=' + this.participantId
this.donationsReceived = null;
this.newDonationsReceived = 0;
this.donationList = [];
this.newDonationList = [];
this.xhr1 = new XMLHttpRequest();
this.xhr2 = new XMLHttpRequest();
this.xhr1.responseType = 'json'
this.xhr2.responseType = 'json'
}
ExtraLifePoller.prototype.pollData = function () {
this.xhr1.open("GET", this.donationsUrl, true)
var obj = this;
this.xhr1.addEventListener('load', function() {
if (obj.xhr1.status == '200') {
var data = obj.xhr1.response
obj.newDonationsReceived = data.length
obj.newDonationList = data
if (obj.donationsReceived == null) {
obj.donationsReceived = data.length;
obj.donationList = data;
}
$('#donationsReceivedCount').html(data.length)
if (obj.newDonationsReceived > obj.donationsReceived) {
// array returned appears to be sorted by most recent firstChild
// if I see enough evidence that that holds, it will be much easier
// to determine what a new donation is.
obj.triggerAlert(setSubtraction(obj.newDonationList, obj.donationList));
obj.donationsRecieved = obj.newDonationsReceived;
obj.donationList = obj.newDonationList;
}
}
});
this.xhr1.send()
this.xhr2.open("GET", this.participantUrl, true)
this.xhr2.addEventListener('load', function() {
if (obj.xhr2.status == '200') {
var data = obj.xhr2.response
$('#goal').html(data.fundraisingGoal)
$('#amountRaised').html(data.totalRaisedAmount)
}
});
this.xhr2.send()
window.setTimeout(function() {ExtraLifePoller.prototype.pollData.call(obj)}, 30000);
}
ExtraLifePoller.prototype.triggerAlert = function (donations) {
for (var i=0; i<donations.length; i++) {
alert(donations[i].donorName + ' ' + donations[i].donationAmount + ' ' + donations[i].message)
}
}
var poller = new ExtraLifePoller();
poller.pollData()