Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap - setInterval() fails to execute

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

like image 540
Phillip Seaman Avatar asked May 10 '26 08:05

Phillip Seaman


1 Answers

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

setInterval(GetCurrentPosition, 3000);
like image 163
dfsq Avatar answered May 11 '26 20:05

dfsq