繁体   English   中英

在JavaScript中检查Internet连接的最佳做法

[英]Best Practise to check Internet connection in javascript

我正在每隔几秒钟使用Ajax检查我的正在使用IE实例的应用程序的Internet连接。 但是由于带宽不足,我的Internet Explorer崩溃了。

可以遵循什么最佳实践来检查Internet连接,以防止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);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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