admin管理员组

文章数量:1357273

I am trying to build a PhoneGap app that updates the Location every few seconds. I have tired to impliment the setInterval() function. But it fails to run!

My code segment looks like this...

// Set time interval and keep excuting every 3 secs
var i = 0;
setInterval(GetCurrentPosition(), 3000); // this will call GetCurrentPosition() each 3 sec

function GetCurrentPosition() {
    // retrieve your info here
    console.log('i = ' + z i);
    i = i + 1;
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

As you can see I added a counter to see if it is firing... it only fires when I call it manually.

Anyone got any suggestions to what I am doing wrong or need to add to correct my error?

Thanks

I am trying to build a PhoneGap app that updates the Location every few seconds. I have tired to impliment the setInterval() function. But it fails to run!

My code segment looks like this...

// Set time interval and keep excuting every 3 secs
var i = 0;
setInterval(GetCurrentPosition(), 3000); // this will call GetCurrentPosition() each 3 sec

function GetCurrentPosition() {
    // retrieve your info here
    console.log('i = ' + z i);
    i = i + 1;
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

As you can see I added a counter to see if it is firing... it only fires when I call it manually.

Anyone got any suggestions to what I am doing wrong or need to add to correct my error?

Thanks

Share Improve this question edited Feb 24, 2015 at 15:15 dfsq 193k26 gold badges242 silver badges259 bronze badges asked Mar 31, 2012 at 19:39 Phillip SeamanPhillip Seaman 311 silver badge3 bronze badges 2
  • If your app goes to the background, your app will be paused and setInterval will not be working, unless you've added in a plugin to keep threads running while the app is in the background. Note that even then there are hard timeouts on how long it will run, as well as possibly breaking app store policies. – tremor Commented Jun 15, 2015 at 15:41
  • Is there an alternative that can be used to replicate the functionality of setInterval? – Marty.H Commented Aug 11, 2015 at 16:10
Add a ment  | 

1 Answer 1

Reset to default 10

The way you use setInterval is incorrect. You should pass the reference to the function like this:

setInterval(GetCurrentPosition, 3000);

本文标签: javascriptPhoneGapsetInterval() fails to executeStack Overflow