[英]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.