最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PhoneGap - setInterval() fails to execute - Stack Overflow

programmeradmin1浏览0评论

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);
发布评论

评论列表(0)

  1. 暂无评论