简体   繁体   中英

Best Practise to check Internet connection in javascript

I am using Ajax to check my internet connection after every few second for my application which is using IE instance. But due to low bandwidth my internet explorer is crashing.

What best practise can be followed to check the internet connection so that it prevent crashing of internet explorer and boost performance ?

I am using the following code to check my internet connection.

The explanation of which is given at: -

http://tomriley.net/blog/archives/111 from where I get the jquery file.

(function ($) {

      $.fn.checkNet = function (intCheckInterval, strCheckURL) {
          if (typeof (intCheckInterval) === 'undefined') {
              var intCheckInterval = 5
          } if (typeof (strCheckURL) === 'undefined') {
              var strCheckURL = window.location
          } else if (strCheckURL.indexOf('http') === -1) {
              var strCheckURL = 'http://' + strCheckURL
          } intCheckInterval = intCheckInterval * 1000; function doRequest(strCheckURL) {
              $.ajax({ url: strCheckURL, cache: false, success: function () {
                  if ($('#netWarn').length) {
                      $('#netWarn').fadeOut()
                  } $('.tempDisabled').removeAttr('disabled').removeClass('tempDisabled')
              }, error: function () {
                  if (!$('#netWarn').length) {


                      $('body').prepend('<p id="netWarn">No internet connection detected, some features will be re-enabled when a connection is detected. </p>'); $('#netWarn').fadeIn()

                  }

              }
              })
          } setInterval(function () {
              doRequest(strCheckURL)
          }, intCheckInterval)
      }
})(jQuery);  

my plugin takes an argument for the length of time between the requests. So, if you want a 10sec interval between requests, call it with this code:

$.fn.checkNet(10);

...after you've included the plugin. I uploaded a new version recently which works much more efficiently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM