4

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

4

1 回答 1

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);
于 2013-09-25T06:55:59.983 回答