检测phonegap应用程序中的慢速互联网连接

时间:2013-09-25 06:49:28

标签: javascript jquery cordova

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

1 个答案:

答案 0 :(得分:2)

您可以使用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);