2012-05-16 23 views
1

我正在使用Ajax檢查我的互聯網連接後每隔幾秒爲我的應用程序正在使用IE實例。但由於低帶寬我的Internet Explorer崩潰。在javascript中檢查互聯網連接的最佳實踐

什麼最佳實踐可以遵循檢查互聯網連接,以防止崩潰的Internet Explorer和提高性能?

我正在使用以下代碼來檢查我的Internet連接。

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

回答

0

我的插件接受請求之間的時間長度的參數。因此,如果您希望請求之間的間隔時間爲10秒,請使用以下代碼進行調用:

$.fn.checkNet(10); 

......包含插件後。我最近上傳了一個新版本,效率更高。