I am developing a webpage that is using cache.manifest to cache relevant files for offline use. However, I need to be able to detect if there is an actual internet connection. if(!navigator.onLine) only works up to a certain degree .. it tends to say it's online when it actually isn't. Therefor I am trying to create an ajax call that returns true if the call is successful, else return fail. I am no master at jQuery, but this is what I have so far. I have tried number of alternatives, but I always end up with an "undefined" return.
function checkIfOnline() {
var returned = true;
$.get('services/getObjects.ashx',
{
"get": "heartbeat",
"hash": userHash,
"uid": uid,
"gid": gid
},
function (data) {
if(data.result === "Success") {
alert("Success");
}
console.log(data.result);
},
"json"
)
.done(function () {alert("done");
returned = true;
return returned;})
.fail(function () {alert("fail");
returned = false;
return returned;});
}
Anyone out there that can see what I am doing wrong? alerts all show what they are supposed to ... I get "Success" alert, and then "done" or "fail" depending on the connection.
Update
Based on the thread that was pointed out, I made the following changes to my code
function checkIfOnline() {
return $.ajax({
type: 'GET',
url: 'services/getObjects.ashx',
data:
{
"get": "heartbeat",
"hash": userHash,
"uid": uid,
"gid": gid
}
});
}
function getOnlineStatus() {
checkIfOnline().done(function(result) {
alert("done - "+result.result);
return true;
}).fail(function() {
alert("failed");
return false;
});
}
But I still get this annoying "undefined",but neither true or false (when calling getOnlineStatus() )