在PhoneGap应用程序中检测缓慢的互联网连接

detect slow internet connection in phonegap app

本文关键字:互联网 连接 缓慢 检测 PhoneGap 应用程序      更新时间:2023-09-26

我能够使用 navigator.network.connection.type 检查我的 phonegap 应用程序中没有互联网时,但是当连接速度很慢(几乎不存在)时,如何使用任何 jquery/javascript 代码检查检测这种情况?

您可以使用

setTimeout()来设置请求的最长时间:

var timeout;
var xhr = $.ajax({
    type: "GET",
    url: "http://myservice.com/jsonp",
    data: data,
    success: function(msg){
       // All went OK
      if(timeout != null) clearTimeout(timeout);
    }
});
timeout = setTimeout(function() {
    // Request took >= 10 seconds
    // We could kill it?
    xhr.abort();
    // Or send a message to the user and ask whether they want to continue
    if(!confirm("Network connection is taking longer than usual to complete. Continue waiting?")) {
      xhr.abort();
    }
}, 10000);