pacokent
pacokent

Reputation: 63

Make setInterval () work for one second loop

I have the following function:

function successFunction(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
             var url= '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng;
             console.log(url);
             $.ajax({
                  type: 'get',
                  url: '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng,
                  success: function (msg) {
                    console.log("Location captured");
                  },
                  processData: false,
              });                  
        }

what it does is that is sends the curren geolocalization of the user that has open that specific page (tab). This is the whole script:

<script type="text/javascript">
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(successFunction);
        } else {
            alert('Parece que la Geolocalización, la cual se requiere para esta página, no está habilitado en su navegador. Por favor, utilice un navegador que lo soporta o active la opción.');
        }
        function successFunction(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
             var url= '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng;
             console.log(url);
             $.ajax({
                  type: 'get',
                  url: '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng,
                  success: function (msg) {
                    console.log("Location captured");
                  },
                  processData: false,
              });                  
        }

      </script>

the bad thing is that its only sending if the user keeps refreshing the page. I´ve been trying to make it loop by it self every second so the GEO sends in real time and the user can be tracked (this is for car geolocalization). Is there a way I can send the info in realtime or how can I make the SetInterval work? this is what I have but it´s not working:

window.setInterval(successFunction, 1000);

Upvotes: 0

Views: 84

Answers (2)

Zohaib Ijaz
Zohaib Ijaz

Reputation: 22885

You have to add an anynomous callback to call actual function with some parameter.

if (navigator.geolocation) {
        window.setInterval(function(){
          navigator.geolocation.getCurrentPosition(successFunction);
        }, 1000);

    } else {
        alert('Parece que la Geolocalización, la cual se requiere para esta página, no está habilitado en su navegador. Por favor, utilice un navegador que lo soporta o active la opción.');
    }
    function successFunction(position) {
        var lat = position.coords.latitude;
        var lng = position.coords.longitude;
         var url= '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng;
         console.log(url);
         $.ajax({
              type: 'get',
              url: '<?php echo url("/") ?>/provider/location/set?lat='+lat+'&lng='+lng,
              success: function (msg) {
                console.log("Location captured");
              },
              processData: false,
          });                  
    }

Upvotes: 1

Daniele Petrarolo
Daniele Petrarolo

Reputation: 458

What about this?

setInterval(function(){
   navigator.geolocation.getCurrentPosition(successFunction);
}, 1000);

Upvotes: 3

Related Questions