简体   繁体   中英

detect slow internet connection in phonegap app

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

You could use setTimeout() to set a maximum time for a request:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM