-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
55 lines (47 loc) · 1.52 KB
/
app.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
(function(window, document){
var request = new XMLHttpRequest(),
$log = document.getElementById('log'),
$requestLog = document.getElementById('requestLog'),
$status = document.getElementById('status'),
$btn = document.getElementById('btn');
/* Open and send request */
function sendRequest () {
request.open('GET', 'http://localhost:8080/', true);
request.onload = function() {
if (request.status >= 200 && request.status < 400){
resp = request.responseText;
}
};
/* Callback in offline state */
request.offlineSave = function () {
$log.innerHTML = ('You are offline. Your changes will be synchronized when you are online.');
};
request.send( 'timestamp=' + Date.now() );
list();
}
/* List the requests data */
function list () {
var rows = '',
data = mydb.find('requests', {}) || [];
data.forEach(function(item){
rows += '<tr>';
rows += '<td>'+ item.id +'</td>';
rows += '<td>'+ item.type +'</td>';
rows += '<td>'+ item.url +'</td>';
rows += '<td>'+ item.data +'</td>';
rows += '</tr>';
});
$requestLog.innerHTML = rows;
}
$btn.addEventListener('click', sendRequest);
list();
/* Check state of connection */
function load () {
$status.innerHTML = navigator.onLine ? 'Online' : 'Offline';
if ( navigator.onLine ) $log.innerHTML = '';
list();
}
load();
window.addEventListener('online', load);
window.addEventListener('offline', load);
}(window, document));