This repository was archived by the owner on May 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathreloadr.js
More file actions
82 lines (72 loc) · 1.75 KB
/
reloadr.js
File metadata and controls
82 lines (72 loc) · 1.75 KB
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
/*
TO USE: include reloadr.js and tell it what to check:
Reloadr.go({
client: [
'/js/main.js',
'/css/layout.css'
],
server: [
'index.php'
],
path: '/reloadr.php',
frequency: 2000
});
All keys are optional. If you don't give client or server, though, it won't do anything.
Shortcut: Reloadr.watch([
'/js/main.js',
'/css/layout.css'
]);
*/
var Reloadr = {
options: {
frequency: 2000,
client: [],
server: [],
path: '/reloadr.php'
},
req: new XMLHttpRequest(),
timeout: null,
watch: function(options) { this.go.call(this, options); },
go: function(options) {
if ( options )
// deal with array being passed
if ( typeof options.length != 'undefined')
this.options.client = options;
// change any options given
else for (x in options)
this.options[x] = options[x];
// set up new timeout
clearTimeout(this.timeout);
this.timeout = setTimeout(function() {
Reloadr.poll.call(Reloadr);
}, this.options.frequency);
},
ajax: function(url, callback) {
this.req.open("GET", url, false);
this.req.setRequestHeader('If-Modified-Since', window._Reloadr_LoadTime.toUTCString());
this.req.send(null);
if (this.req.status == 200)
callback.call(
Date.parse(
this.req.getResponseHeader('Last-Modified')
)
);
},
poll: function(options) {
var urls = this.options.client.slice();
// build url for server-side files
if ( this.options.server.length )
urls.push(this.options.path +'?'+ this.options.server.join(','));
// check 'em
for (i in urls)
this.ajax.call(this, urls[i], function() {
if (this > Date.parse(window._Reloadr_LoadTime))
location.reload();
});
this.go();
},
init: function(options) {
window._Reloadr_LoadTime = new Date();
}
};
Reloadr.init();