JQuery Ajax - How to Detect Network Connection error when making Ajax call

前端 未结 8 2218
闹比i
闹比i 2020-11-27 04:02

I have some Javascript JQuery code that does an Ajax call to the server every 5 mins, it\'s to keep the server session alive and keep the user logged in. I\'m using $.

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 04:45

    here's what I did to alert user in case their network went down or upon page update failure:

    1. I have a div-tag on the page where I put current time and update this tag every 10 seconds. It looks something like this:

      22:09:10

    2. At the end of the javascript function that updates the time in the div-tag, I put this (after time is updated with AJAX):

      var new_value = document.getElementById('reloadthis').innerHTML;
      var new_length = new_value.length;
      if(new_length<1){
          alert("NETWORK ERROR!");
      }
      

    That's it! You can replace the alert-part with anything you want, of course. Hope this helps.

提交回复
热议问题