0

I check my Internet connection with this function and call it periodic using timeinterval()

function check_dc(){
clearInterval(id);
id=setInterval("check_dc()",5000);
$.ajax({
url: 'http://www.google.com/',
type: 'GET',
error: function(){
    flag=0;
    alert("DC");
},
success: function(res, textStatus, xhr) {
    var headline = $(res.responseText).find('a.tsh').text();
    flag=1;
    alert((res.responseText));
}
});
}

I set a flag if my request successes and reset it when failed. But when I disconnect from Internet to test the error function I can't see the alert also when I add beforeSend attribute just like error, I can't see it either!

p.s: I use jquery.xdomainajax to have cross domain request

pooya
  • 901
  • 2
  • 15
  • 29
  • You can't get alerts from a callback. Use the console instead. – nroose Nov 19 '14 at 00:33
  • Possible duplicate of [Determine if $.ajax error is a timeout](https://stackoverflow.com/questions/3543683/determine-if-ajax-error-is-a-timeout) – Liam Sep 14 '18 at 11:02

1 Answers1

-1

I'm assuming that when you disconnect from the internet, the request just hangs? Try specifying a timeout inside the settings for your call to jQuery.ajax

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

DMac the Destroyer
  • 5,240
  • 6
  • 36
  • 56