phonegap Jquery check data connection before request -


I have jquery code for the server every 30 seconds to send the current coordinates. It is good in areas with good data coverage signals However, it crashes when the signal is fluctuating (data coverage is available and frequently disappears). After some time the app simply crashes without any warning ... catching section.

I have to check whether data coverage is available or not, then send coordinates to the server continuously, otherwise do not just check again Data coverage is available.

Here's my code:

  & lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Script type = "text / javascript" charset = "utf-8" src = "cordova-2.0.0 js" & gt; & Lt; / Script & gt; & Lt; script src = "jquery-1.5.2.min.js" & gt; & Lt; / Script & gt; & Lt; script src = "main.js" & gt; & Lt; / Script & gt; & Lt; Script src = "jquery.mobile-1.0a4.min.js" & gt; & Lt; / Script & gt; & Lt; Script & gt; Var netCoverage = false; Var lat = ''; Var lon = ''; Document.addEventListener ("deviceready", onDeviceReady, incorrect); Document.addEventListener ("Pause", OnDevice Reid, False); Function check connection () {var networkState = navigator.network.connection.type; Var state = {}; States [connection. Uncertain] = 0; // 'Unknown Connection'; States [connection. Ethernet] = 1; // 'Ethernet connection'; States [connection. FII] = 2; //'Wi-Fi connection'; States [connection. CELL_2G] = 3; // 'cell 2G connection'; States [connection. CELL_3G] = 4; // 'cell 3G connection'; States [connection. CELL_4G] = 5; // 'cell 4g connection'; States [connection. NONE] = 6; // 'No Network Connection'; // Alert ('Connection Type:' + State [NetworkTat]); If (state [networktate]! = 0 and state [networktate]! = 6) net quote = true; Other Pure Convention = Wrong; } // The network connection ends for watchID = null; Function Online Readie () {checkConnection (); Get the most accurate position updates available on // // Tools. Var option = {Max Edge: 30000, High High Accuracy: true}; Clock id = navigator.jolocation.watchpace (onsut, on error, options); // update every 3 seconds workID = tap; } // onError callback receives a statusAir object / / function error (error) on {warning ('code:' + error.code + '\ n' + 'Message:' + error.message + '\ n' ); } $ (Document) .ready (function) () {/ *************************************************************************************** ************ {If (netCoverage) {$ .ajax ({url: 'http://www.myurl.com/add_position.php?&lat=' + Latitude '& amp; ; Lon = '+ lon, success: function (data) {$ ("# Geo_info") .text (data); accIndex = server; reset after sending to server;} error: function (request, error) {$ ( "# Geo_info"). Text ("network error:" + error); $ .ajax (this); return;}, full: function () {// full schedule of the current time of the schedule when the current set time (worker, 30000 );}})}} / Connection other (commandal data caps // After the call to the method some time; // the app is crashing; (otherwise, 'unknown or no network connection.'); SetTimeout (worker, 30000); // no warning}} hold (mistake) {$ ("# geo_info"). Text ('Worrying,' + err.message); $ .ajax (this); return;}}) (); / *** ******************************************************************************************** **************************************************************************************************** & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; P id = "geo_info" & gt; View geo location ... & lt; / P & gt; & Lt; Button onclick = "cleanup ();" & Gt; Clear Watch & lt; / Button & gt; & Lt; / Body & gt; & Lt; / Html & gt;    

why not & amp; Event?

Online

This event fire when an application goes online, and the device connects to the internet.

  document. AddEventListener ("online", yourCallbackFunction, incorrect);   

Offline

When an app goes offline, the event gets burnt, and the device is not connected to the internet.

  document.addEventListener ("offline", yourCallbackFunction, incorrect);   

Hope this is useful for you!

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -