-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
executable file
·91 lines (79 loc) · 1.85 KB
/
api.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
(function($) {
var Poller = function () {
this.defaults = {
type: 'veggies',
limit: 10
};
this.items = {
veggies: [
'Adzuki Beans',
'Asparagus',
'Black-eyed Peas',
'Brussels Sprouts',
'Carrots',
'Collard Greens',
'Parsnips',
'Rhubarb',
'Yams',
'Watercress',
'Apricots',
'Blackcurrants',
'Cherimoya',
'Dates',
'Elderberry',
'Guava',
'Kumquat',
'Miracle Fruit',
'Purple Mangosteen',
'Satsuma'
],
fruits: [
'Apricots',
'Blackcurrants',
'Cherimoya',
'Dates',
'Elderberry',
'Guava',
'Kumquat',
'Miracle Fruit',
'Purple Mangosteen',
'Satsuma'
]
};
};
Poller.prototype._getRandomNumber = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
Poller.prototype._getData = function (type) {
var item, i, len;
var list = this.items[type] || [];
var results = [];
for (i = 0, len = list.length; i < len; i++) {
item = list[i];
results.push({
name: item,
count: this._getRandomNumber(0, 200000)
});
}
return results;
};
Poller.prototype._processData = function (data, limit) {
return data.slice(0, limit);
};
Poller.prototype.poll = function (options, cb) {
var self = this;
var config = $.extend({}, this.defaults, options);
var dfd = $.Deferred();
setTimeout(function () {
var payload = self._processData(self._getData(config.type), config.limit);
cb && cb(payload);
dfd.resolve(payload);
}, this._getRandomNumber(400, 2000));
return dfd;
};
if (window.spredfast == null) {
window.spredfast = {
Poller: Poller
};
}
}(jQuery));