forked from RobertFischer/JQuery-PeriodicalUpdater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.periodicalupdater.js
executable file
·266 lines (245 loc) · 9.1 KB
/
jquery.periodicalupdater.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* PeriodicalUpdater - jQuery plugin for timed, decaying ajax calls
*
* http://www.360innovate.co.uk/blog/2009/03/periodicalupdater-for-jquery/
* http://enfranchisedmind.com/blog/posts/jquery-periodicalupdater-ajax-polling/
*
* Copyright (c) 2009-2012 by the following:
* Frank White (http://customcode.info)
* Robert Fischer (http://smokejumperit.com)
* 360innovate (http://www.360innovate.co.uk)
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function ($) {
// The free version
$.PeriodicalUpdater = function(url, options, callback){
if(!options) options = {};
var settings = jQuery.extend(true, {
url: url, // URL of ajax request
cache: false, // By default, don't allow caching
method: 'GET', // method; get or post
data: '', // array of values to be passed to the page - e.g. {name: "John", greeting: "hello"}
minTimeout: 1000, // starting value for the timeout in milliseconds
maxTimeout:64000, // maximum length of time between requests
multiplier: 2, // if set to 2, timerInterval will double each time the response hasn't changed (up to maxTimeout)
maxCalls: 0, // maximum number of calls. 0 = no limit.
maxCallsCallback: null, // The callback to execute when we reach our max number of calls
autoStop: 0, // automatically stop requests after this many returns of the same data. 0 = disabled
autoStopCallback: null, // The callback to execute when we autoStop
cookie: false, // whether (and how) to store a cookie
runatonce: false, // Whether to fire initially or wait
verbose: 0 // The level to be logging at: 0 = none; 1 = some; 2 = all
}, options);
var pu_log = function (msg, lvl) {
lvl = lvl || 1;
if(settings.verbose >= lvl) {
try {
console.log(msg);
} catch (err) { }
}
};
// set some initial values, then begin
var timer = null;
var remoteData = null;
var prevData = null;
var timerInterval = settings.minTimeout;
var maxCalls = settings.maxCalls;
var autoStop = settings.autoStop;
var calls = 0;
var noChange = 0;
var originalMaxCalls = maxCalls;
// Function to reset the timer to a given time
var reset_timer = function (interval) {
$(function() { // Ensure we're live
if (timer !== null) {
clearTimeout(timer);
}
timerInterval = interval;
pu_log('resetting timer to ' + timerInterval + '.', 2);
if(settings.cookie && $.cookie) {
$.cookie(settings.cookie.name, timerInterval, settings.cookie);
}
timer = setTimeout(getdata, timerInterval);
});
};
// Function to boost the timer
var boostPeriod = function () {
if (settings.multiplier > 1) {
var before = timerInterval;
timerInterval = timerInterval * settings.multiplier;
if (timerInterval > settings.maxTimeout) {
timerInterval = settings.maxTimeout;
}
pu_log('adjusting timer from ' + before + ' to ' + timerInterval + '.', 2);
}
reset_timer(timerInterval);
};
// Handle the cookie config
if(settings.cookie) {
if(typeof(settings.cookie) == 'boolean') {
settings.cookie = {
name: url
};
} else if(typeof(settings.cookie) != 'object') {
settings.cookie = {
name: settings.cookie.toString()
};
}
if(!settings.cookie.name) {
settings.cookie.name = url;
}
if(!$.cookie) {
$.getScript("//rawgit.com/carhartl/jquery-cookie/master/jquery.cookie.js", function() {
pu_log("Loaded the cookies handler script", 2);
if($.cookie(settings.cookie.name)) {
pu_log("Not runatonce and have cookie value", 2);
reset_timer($.cookie(settings.cookie.name));
} else {
pu_log("Not runatonce, but no cookie value", 2);
reset_timer(timerInterval);
}
}).fail(function() {
pu_log("Could not load the cookies handler script", 1);
reset_timer(timerInterval);
});
} else {
if($.cookie(settings.cookie.name)) {
reset_timer($.cookie(settings.cookie.name));
}
}
}
// Construct the settings for $.ajax based on settings
var ajaxSettings = jQuery.extend(true, {}, settings);
if (settings.type && !ajaxSettings.dataType) { ajaxSettings.dataType = settings.type; }
if (settings.sendData) { ajaxSettings.data = settings.sendData; }
ajaxSettings.type = settings.method; // 'type' is used internally for jQuery. Who knew?
ajaxSettings.ifModified = true;
// Create the function to get data
var getdata = function(force) {
var toSend = jQuery.extend(true, {}, ajaxSettings); // jQuery screws with what you pass in
if (typeof (options.data) == 'function') {
toSend.data = options.data();
}
if (toSend.data) {
// Handle transformations (only strings and objects are understood)
if (typeof (toSend.data) == "number") {
toSend.data = toSend.data.toString();
}
}
if (force || maxCalls === 0) {
pu_log("Sending data");
$(function() { $.ajax(toSend); });
} else if (maxCalls > 0 && calls < maxCalls) {
pu_log("Sending data because we are at " + calls + " of " + maxCalls + " calls");
$(function() { $.ajax(toSend); });
calls++;
} else if(maxCalls == -1) {
pu_log("NOT sending data: stop has been called", 1);
} else {
pu_log("NOT sending data: maximum number of calls reached - " + maxCalls, 1);
if (settings.maxCallsCallback) { settings.maxCallsCallback(); }
}
}
var handle = {
boostTimer: function(mag) {
if(mag > 0 && mag != 1) {
pu_log("Boosting timer by a factor of " + mag);
timerInterval = timerInterval * mag;
} else {
pu_log("Cannot boost timer by a factor of " + mag);
}
reset_timer(timerInterval);
return;
},
restart: function (newInterval) {
pu_log("Calling restart");
maxCalls = originalMaxCalls;
calls = 0;
noChange = 0;
reset_timer(newInterval || timerInterval);
return;
},
send: function() {
pu_log("Explicit call to send");
getdata(true);
return;
},
stop: function () {
pu_log("Calling stop");
maxCalls = -1;
if(settings.cookie && $.cookie) {
$.cookie(settings.cookie.name, null, settings.cookie);
}
return;
}
};
// Implement the tricky comparison logic
ajaxSettings.success = function (data) {
pu_log("Successful run! (In 'success')", 2);
remoteData = data;
};
ajaxSettings.complete = function (xhr, success) {
pu_log("Status of call: " + success + " (In 'complete')", 2);
if (success == "success" || success == "notmodified") {
var rawData = $.trim(xhr.responseText);
if (prevData == rawData) {
if (autoStop > 0) {
noChange++;
if (noChange == autoStop) {
handle.stop();
if (settings.autoStopCallback) { settings.autoStopCallback(noChange); }
return;
}
}
boostPeriod();
} else {
noChange = 0;
reset_timer(settings.minTimeout);
prevData = rawData;
if (settings.cookie) $.cookie(settings.cookie.name, prevData, settings.cookie);
if (remoteData === null) { remoteData = rawData; }
// jQuery 1.4+ $.ajax() automatically converts "data" into a JS Object for "type:json" requests now
// For compatibility with 1.4+ and pre1.4 jQuery only try to parse actual strings, skip when remoteData is already an Object
if ((ajaxSettings.dataType === 'json') && (typeof (remoteData) === 'string') && (success == "success")) {
remoteData = JSON.parse(remoteData);
}
if (settings.success) { settings.success(remoteData, success, xhr, handle); }
if (callback) { callback(remoteData, success, xhr, handle); }
}
}
if (settings.complete) { settings.complete(xhr, success); }
remoteData = null;
};
ajaxSettings.error = function (xhr, textStatus) {
pu_log("Error message: " + textStatus + " (In 'error')", 2);
if(textStatus != "notmodified") {
prevData = null;
if(settings.cookie) $.cookie(settings.cookie.name, null, settings.cookie);
reset_timer(settings.minTimeout);
}
if(settings.error) { settings.error(xhr, textStatus); }
};
// Make the first call
if (settings.runatonce) {
pu_log("Executing a call immediately", 1);
getdata(true);
} else if($.cookie && settings.cookie && $.cookie(settings.cookie.name)) {
// Do nothing (already handled above)
} else {
pu_log("Enqueing a the call for after " + timerInterval, 1);
reset_timer(timerInterval);
}
return handle;
};
// The bound version
$.fn.PeriodicalUpdater = function(url, options, callback) {
var me = this;
return $.PeriodicalUpdater(url, options, function() {
return callback.apply(me, arguments);
});
};
})(jQuery);