I've got next code that fills page with data I get from server every 10 sec:
var refreshFunction = function() {
//get data from server
$.get("<c:out value="${baseURL}"/>/js/getCounts", function(data) {
vardata = data;
});
if (vardata != null) {
//divide
countArray = vardata.split("/");
//if server returns "-" or new array with new structure
if ((vardata == "-" || length != countArray.length)
&& !(document.getElementById('requestForm') instanceof Object)) {
//reload
location.reload();
clearInterval(refreshId);
} else {
//fill page with data
for (var j = 0; j <= countArray.length; j++) {
console.log(countArray[j]);
$('#badge_' + (j + 1)).text(countArray[j]);
}
}
}
};
$(document).ready(function() {
refreshId = setInterval(refreshFunction, 10000);
});
The code works, but if I open application and then turn off my server, script will never stop. I'm having
Failed to load resource: net::ERR_CONNECTION_REFUSED
How can I catch it and stop the script after that? I was trying to wrap code blocks with try and catch(e), but doesn't help.