Question

I am running a single webpage 24/7 on two computers. One computer is running IE8, the other IE9. On this webpage is a javascript timer which runs $.getJSON to retrieve a cross-domain JSON object. The $.getJSON works perfectly under normal conditions however there is the possibility that the internet connection on one of these computers will go down temporarily. Since I am using the $.getJSON to retrieve new content for the webpage, if the internet goes down momentarily, the old content will be shown.

My issue is that I assumed $.getJSON's fail event would fire if the internet went down when the $.getJSON was called. In this case, a new timer would be set to attempt to retrieve the JSON in X minutes (it will never stop trying). When testing this, I disabled my internet connection and yet the code within the fail event did not fire.

Will fail not be called in the case of no internet connection? If so, what would you recommend to prevent my JavaScript from stopping permanently when the internet goes down?

(One method I looked at was checking for an internet connection before the JSON call however I've read window.navigator.onLine is unreliable and I couldn't find any other solutions)

Was it helpful?

Solution

getJSON is just shorthand for ajax with a few values already set. And ajax has a timeout option. That is probably the most reliable option (but obviously won't respond to network failure immediately). I personally would combine it with any checks you can find for determining network status.

$.ajax({
    dataType: "json",
    url: url,
    data: data,
    success: success,
    timeout: 10000 // 10 seconds
});

http://api.jquery.com/jQuery.getJSON/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top