running watchposition in background

I am making a browser based html5 app for mobile phones that sends out it's lat and lng to a database on the web after it gets it using the html5 watchposition method.

When the app is not in the foreground, watchposition method appears to stop running, or at least it stops setting new values for global lat lng variables that it is supposed to set before those variable values get sent to the database via a jquery $.get.

I understand that javascript interval functions and things like that might not work when the browser is in the background in the case of a mobile phone. However, it does manage to keep performing it's $.get in the background during the function interval, and the database shows a new time value in the database every 10 seconds. So if the time value has changed, I know it is still doing successful get calls in the background.  

However, the lat and lng values do not change, which means that watchposition must stop setting the lat lng global variable values when the browser is in the background. So why would one part of javascript ($.get) keep running but watchposition does not?

Is there any solution to this problem that anyone knows of? If not, does anybody think it's a good workaround to create a very simple native app that does nothing but send out it's location and since it is native it can be kept alive? Is it possible to make a simple native app like that which does nothing but start sending out location, and also launches my web app url in the browser with one click?

Any other ideas like hybrids, etc... would be welcome as well. I'm really stuck on this one. Phonegap.... Webviews might be a possibility, but it would be a shame to go through all of that when all that I need to stay alive is just location capability and sending. The rest of the stuff in my app can sleep.
LVL 9
BobHavertyComhAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eoin OSullivanConsultantCommented:
Phonegap is probably your best option as background JavaScript needs an app wrapper to access the gps over an extended period. At least Phonegap would allow you to reuse all your web code rather than having to build a completely native iOS app
See the following article which explains it very well
http://raibledesigns.com/rd/entry/phonegap_to_the_rescue
0
BobHavertyComhAuthor Commented:
Hi and thanks for your response. Yes phonegap looks like a very viable possibility and thanks for the link. I also might just make a very simple app that sends out the phone's lat lng every 10 seconds in the background and does nothing else. It would only have one button, which would launch the web app url in the default browser. Everything else about this web app works great, and and all I need is a simple lat lng to be sent out in the background. So with no other needs, maybe phonegap might be too much or not necessary.

Here's the weird part. The web app is not supposed to perform any persistent javascript tasks in the background as I understand things. However in chrome for android, it IS still executing and performing a successful get call every 10 seconds. So why does the get call still go on, but watchposition stops? Something to do with a timer? Here's my code

$(document).ready(function(){
    initLocationProcedure();
    $.get(encodeURI("latlng.aspx?lat=" + lat + "&lng=" + lng));
    getHomeLocation();
    setInterval(function () {
        if (lat != null && lat != "" && lat != "undefined") { 
            $.get(encodeURI("latlng.aspx?lat=" + lat + "&lng=" + lng));
        } 
        else {
            alert("Position is not defined");
        } 
    }, 10000);})

Open in new window


Here's the code that is supposed to keep changing the global lat lng values that the set interval function uses to send out
function watchCurrentPosition() {
    var positionTimer = navigator.geolocation.watchPosition(function (position) {
            setMarkerPosition(currentPositionMarker, position);
            lat = position.coords.latitude;
            lng = position.coords.longitude;
            if (markers.length > 1) {
                for (i = 0; i < markers.length; i++) {
                    bounds.extend(markers[i].getPosition());
                }
                map.fitBounds(bounds);
            }
            else {
                map.panTo(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
            }
        },
        function geo_error(error) {
            switch(error.code) {
                case error.TIMEOUT:
                    alert('Geolocation Timeout');
                    break;
                case error.POSITION_UNAVAILABLE:
                    alert('Geolocation Position unavailable');
                    break;
                case error.PERMISSION_DENIED:
                    alert('Geolocation Permission denied');
                    break;
                default:
                    alert('Geolocation returned an unknown error code: ' + error.code);
            }
        },
        {
            enableHighAccuracy: true,
            maximumAge: 0,
            timeout: 30000
        });
}

Open in new window

0
Eoin OSullivanConsultantCommented:
As far as I understand .. javascript performance while the browser is in the background is not 100% stopped but timers and timeouts are common and overall javascript perfomance is unreliable but I'm pretty sure that switching app from foreground to background does not instantly stop all scripts or force them to stop.

That is why when you background the browser your scripts still run for a time at least.

Polling the hardware however is more strict and therefore the geolocation fails pretty much immediately after you background the browser.

By all accounts different browsers have slightly different javascript implementations and Safari, Chrome and Opera all behave differently in terms of javascript persistence when backgrounded.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
That was what I was looking for. I wanted to understand the "why" part, and your explanation makes perfect sense. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.