-
Notifications
You must be signed in to change notification settings - Fork 50
/
jquery.updater.js
46 lines (39 loc) · 1.5 KB
/
jquery.updater.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
/**
* Updater - jQuery plugin for timed ajax calls
*
* Based on PeriodicalUpdater (http://github.com/RobertFischer/JQuery-PeriodicalUpdater/tree/master)
*
* Copyright (c) 2009 by the following:
* * 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
*
* Version: 1.0
*/
(function($) {
$.Updater = function(url, options, callback){
var settings = jQuery.extend(true, {
url: url, // URL of ajax request
method: 'get', // method; get or post
data: '', // array of values to be passed to the page - e.g. {name: "John", greeting: "hello"}
type: 'json', // response type - text, xml, json etc
interval: '3000'
}, options);
var timerInterval = settings.interval;
// Construct the settings for $.ajax based on settings
var ajaxSettings = jQuery.extend(true, {}, settings);
ajaxSettings.dataType = settings.type;
ajaxSettings.type = settings.method; // 'type' is used internally for jQuery. Who knew?
ajaxSettings.success = function(data) {
PeriodicalTimer = setTimeout(getdata, timerInterval);
if(callback) {
callback(data);
}
};
// Make the first call
$(function() { getdata(); });
function getdata() { $.ajax(ajaxSettings); }
};
})(jQuery);