0

Hi I have a piece of javascript code which i want to call every two minutes, however i can not seem to get it to work, when the page loads for the first time it works fine, but it doesn't update after that.

Please see code below:

 function position(){
 var a=setTimeout(position,60000);
 }


if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(function(position)
{
  var lat = position.coords.latitude;
  var lon = position.coords.longitude;
   var xmlHttp = new XMLHttpRequest();  //not the cross browser way of doing it
   xmlHttp.open("GET", "locator/test1.php?lat=" + lat + "&lon=" + lon, true); 
   xmlHttp.send(null);

   });
}

Thanks

Sam Williams
  • 175
  • 2
  • 3
  • 12

7 Answers7

2

The code is running once because it is not in the function, but never again because you are never starting the timeout. I think you want to restructure like this:

function position() {
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var lat = position.coords.latitude;
      var lon = position.coords.longitude;
      var xmlHttp = new XMLHttpRequest();  //not the cross browser way of doing it
      xmlHttp.open("GET", "locator/test1.php?lat=" + lat + "&lon=" + lon, true); 
      xmlHttp.send(null);    
    });
  }

  // fire again in 120000ms (2 minutes)
  setTimeout(position, 120000);
}

// fire the initial call
position()  ;
Brenton Alker
  • 8,947
  • 3
  • 36
  • 37
0
var intervalId = setInterval(function() {
  position();
}, 12e5);
Eru
  • 675
  • 4
  • 8
0

In the code you posted, the geolocation functionality isn't actually part of the position function

and position is never called

Seth
  • 558
  • 3
  • 6
0

setTimeout will only run the function once after the specified time is passed. If you want to repeat the function, use setInterval.

See 'setInterval' vs 'setTimeout' for detail

Community
  • 1
  • 1
Pitamber Tiwari
  • 536
  • 1
  • 6
  • 19
0
function position(){

  if(navigator.geolocation)
  {
      navigator.geolocation.getCurrentPosition(function(position)
  {
   var lat = position.coords.latitude;
   var lon = position.coords.longitude;
   var xmlHttp = new XMLHttpRequest();  //not the cross browser way of doing it
   xmlHttp.open("GET", "locator/test1.php?lat=" + lat + "&lon=" + lon, true); 
   xmlHttp.send(null);
}

var a=setInterval(position,120000);
Ryan
  • 5,644
  • 3
  • 38
  • 66
0

you can try setInterval to do it https://developer.mozilla.org/en-US/docs/DOM/window.setInterval

but if you prefer to use setTimeout you need to call your function recursively.

if(navigator.geolocation){
 function doSomethingWithPosition(){
  navigator.geolocation.getCurrentPosition(function(position){
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    var xmlHttp = new XMLHttpRequest();  //not the cross browser way of doing it
    xmlHttp.open("GET", "locator/test1.php?lat=" + lat + "&lon=" + lon, true); 
    xmlHttp.send(null);
  });
  //RECURSIVE CALL
  setTimeout(doSomethingWithPosition, 60000);
 }

 //FIRST CALL
 doSomethingWithPosition();
}
Scipion
  • 1,350
  • 1
  • 10
  • 16
0

I also have a suggestion.

I think you want to do this - I use jQuery because of the better Ajax callback functionality

if(navigator.geolocation) {
  getLoc();
}
function getLoc() {
  navigator.geolocation.getCurrentPosition(function(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    $.get("locator/test1.php?lat=" + lat + "&lon=" + lon, 
      function(data){ 
        // do something with data returned
        setTimeout(getLoc,120000); // call again in 2 minutes
      }
    ); 
  });
}

which will call locator every 2 minutes assuming it successfully calls it the first time

mplungjan
  • 169,008
  • 28
  • 173
  • 236