http://code.google.com/p/glazkov-attic/source/browse/trunk/YourTimesheets/Scripts/monitor.js
:DG<
====
function monitorOnlineness(onlinenessChanged) {
var interval = 1000; // check network once a second
var onlineness = null;
var timer = google.gears.factory.create('beta.timer');
var timerId = null;
function check() {
timerId = timer.setTimeout(function() {
var req = google.gears.factory.create('beta.httprequest');
req.onload = function() { onlinenessChanged(true); }
req.onerror = function() { onlinenessChanged(false); }
}, interval);
}
function updateOnlineness(val) {
if (onlineness !== val) {
onlineness = val;
onlinenessChanged(val);
}
check();
}
function cancel() {
if (timerId) {
timer.clearTimeout(timerId);
}
}
return cancel;
}
// to use:
var cancel = monitorOnliness(function(isOnline) {
console.log(isOnline ? 'online!' : 'offline :(');
});
// to cancel:
cancel();
===
- a
Argh, there needs to be an initial call to check() at the bottom of
monitorOnliness().
- a
:DG<
(there needs to be a url param to monitorOnliness() and an
req.open("GET", url) and req.send(null) in check())..
- a
Methink HEAD request is better, because server will know we're just
checking the pulse.
Good point.
- a