Tuesday, 3 June 2014

Check for Internet connection periodically in phonegap using java script

Check for Internet connection periodically in phonegap using java script

i used here time intervel it will check for every 10sec if connection is lost it will show alert that connection is deactivatedand if connection is gained it will show alert that connection is activated.

for this u have to include internet connection plugins and this is for both ios and android.


<script>
            var internetAvailableCount = 1;
            var internetUnavailableCount = 0 ;
            var conn=false;
            setInterval(function(){
                        var isConnected = checknetconnection(); // checkConnection() comes from above code
                        if(conn == false)
                        {
                        if(internetUnavailableCount == 0)
                        {
                        navigator.notification.alert(
                                                     'internet connection is deactivated'// message
                                                     alertDismissed,         // callback
                                                     'Internet',            // title
                                                     'Ok'                  // buttonName
                                                     );
                        internetUnavailableCount = internetUnavailableCount +1;
                        internetAvailableCount = 0;
                        }
                        return false;
                        }
                        else
                        {
                        if(internetAvailableCount == 0)
                        {
                        navigator.notification.alert(
                                                     'internet connection is activated'// message
                                                     alertDismissed,         // callback
                                                     'Internet',            // title
                                                     'Ok'                  // buttonName
                                                     );
                        internetUnavailableCount = 0;
                        internetAvailableCount = 1;
                        
                        }
                        
                        }
                        }, 10000);

            </script>


  

function checknetconnection()
{

if(navigator.network.connection.type == Connection.NONE)
{
   
    conn = false;
}
    
else
{
    conn = true;
    return true;
}

}


No comments:

Setting Up Multiple App Targets in Xcode from a Single Codebase

 To create two different apps (like "Light" and "Regular") from the same codebase in Xcode, you can follow these steps b...