Ramakrishna
Ramakrishna

Reputation: 686

detect slow internet connection in phonegap app

I'm able to check When there is no internet in my phonegap app using navigator.network.connection.type but when the connection is slow(almost not there) how to check detect such situation using any jquery/javascript code??

Upvotes: 4

Views: 2625

Answers (1)

CodingIntrigue
CodingIntrigue

Reputation: 78595

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

Upvotes: 2

Related Questions