简体   繁体   English

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

[英]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. 我正在每隔几秒钟使用Ajax检查我的正在使用IE实例的应用程序的Internet连接。 But due to low bandwidth my internet explorer is crashing. 但是由于带宽不足,我的Internet Explorer崩溃了。

What best practise can be followed to check the internet connection so that it prevent crashing of internet explorer and boost performance ? 可以遵循什么最佳实践来检查Internet连接,以防止Internet Explorer崩溃并提高性能?

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

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: 因此,如果您希望两次请求之间的间隔为10秒,请使用以下代码进行调用:

$.fn.checkNet(10);

...after you've included the plugin. ...包括插件后。 I uploaded a new version recently which works much more efficiently. 我最近上传了一个新版本,它的工作效率更高。

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

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