title: How to check internet connection in Phonegap periodically?
body: <p>I am working with Phonegap. I need to check the network connection periodically. Actually I am getting some data from a server. If there is no connection, I need to show an error alert.</p>
  
  <p>I googled it and found the solution. But it is not ok. Because I need to check the connection periodically.</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;navigator.network.connection.type Example&lt;/title&gt;
      &lt;script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" charset="utf-8"&gt;
      // Wait for PhoneGap to load
      document.addEventListener("deviceready", onDeviceReady, false);
      function onDeviceReady() {
          checkConnection();
      }
  
      function checkConnection() {
          var networkState = navigator.network.connection.type;
          var states = {};
          states[Connection.UNKNOWN]  = 'Unknown connection';
          states[Connection.ETHERNET] = 'Ethernet connection';
          states[Connection.WIFI]     = 'WiFi connection';
          states[Connection.CELL_2G]  = 'Cell 2G connection';
          states[Connection.CELL_3G]  = 'Cell 3G connection';
          states[Connection.CELL_4G]  = 'Cell 4G connection';
          states[Connection.NONE]     = 'No network connection';
          alert('Connection type: ' + states[networkState]);
      }
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;p&gt;A dialog box will report the network state.&lt;/p&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It check only the first time when the application launches. But I need to check it periodically, because I am doing socket programming. If there is a problem with internet, I need to show it. But this code only shows at launch time.</p>