在javascript中检查互联网连接的最佳实践

Best Practise to check Internet connection in javascript

本文关键字:最佳 连接 互联网 javascript 检查      更新时间:2023-09-26

我正在使用Ajax每隔几秒钟检查一次我的互联网连接,因为我的应用程序正在使用IE实例。但是由于带宽低,我的IE浏览器崩溃了。

可以遵循哪些最佳实践来检查互联网连接,以防止Internet Explorer崩溃并提高性能?

我正在使用以下代码来检查我的互联网连接。

其解释如下: -

http://tomriley.net/blog/archives/111 我从哪里获得jquery文件。

(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);  

我的插件对请求之间的时间长度进行参数。因此,如果您希望请求之间的间隔为 10 秒,请使用以下代码调用它:

$.fn.checkNet(10);

。包含插件后。我最近上传了一个新版本,它的效率更高。