"Online" and "Offline" events doesn't give immediate response...

  • 1
  • Problem
  • Updated 5 years ago
Hi Team,

I am doing one application(phone gap+android) for that I have to check internet "online" or "offline" ,for the way I followed phone gap API documentation,but for getting response its taking long time(few minutes).

how can I get immediate response from online and offline events. Please give solution for this.

Please tell me solution for this.

please below my code:

function init() {
document.addEventListener("deviceready", onDeviceReady, true);
}

function onDeviceReady(){
document.addEventListener("online", onOnline, false);
document.addEventListener("offline", onOffline, false);
}

function onOnline() {
alert("yes-onOnline");
}
function onOffline() {
alert("yes-onOffline");
}

Thank you,
Siva.
Photo of gudluri siva

gudluri siva

  • 62 Posts
  • 0 Reply Likes

Posted 8 years ago

  • 1
Photo of Andrew Lunny

Andrew Lunny

  • 1911 Posts
  • 199 Reply Likes
The offline/online events only fire when the state changes - when your app goes from offline to online, or vice versa.

If you need to find out whether your app is on or offline immediately, check navigator.network.connection.type after deviceready has fired
http://docs.phonegap.com/en/2.1.0/cor...
Photo of gudluri siva

gudluri siva

  • 62 Posts
  • 0 Reply Likes
Hi Andrew Lunny,

Thank you for your kind response.

Thank you,
Siva.
Photo of Iván Pallarés

Iván Pallarés

  • 2 Posts
  • 0 Reply Likes
Is there any reason why navigator.network does not return an object?I am using it after deviceready to retrieve navigator.network.connection.type but I am getting a:

"Cannot read property 'connection' of undefined "

error.

Any clue?

Thanks!!
Photo of Andrew Lunny

Andrew Lunny

  • 1911 Posts
  • 199 Reply Likes
Iván: that API has changed, and you should be referring to navigator.connection. Please see the documentation here:
http://docs.phonegap.com/en/2.2.0/cor...
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
Yes, well, use that plugin. It will give that information.
You can use it to either plainly check for a connection, or react to connection status changes instantly.
Photo of Gayathri

Gayathri

  • 70 Posts
  • 0 Reply Likes
I need html extension...for checking
Photo of Petra V.

Petra V., Champion

  • 7794 Posts
  • 1391 Reply Likes
*sigh*
No, you don't.
Look, you asked 45 minutes ago in a 3-year-old thread, which is bad enough. By now, you could have referenced that plugin, coded the required one-liner and rebuilt. Twice.
Why don't you just do that?
Photo of Gayathri

Gayathri

  • 70 Posts
  • 0 Reply Likes
Thanks for ur reply
Photo of Gayathri

Gayathri

  • 70 Posts
  • 0 Reply Likes
<!DOCTYPE html SYSTEM>

Cordova Online Example

I got an error in this statement ( states[Connection.UNKNOWN] = 'Unknown connection';)