-1

I've been trying to update the latitude and longitude from an API. The API changes its data on real time. I tired using jQuery with an interval of 1000, but unfortunately it didn't fetch data from the API on the interval time given. It fetches the JSON for the first time but does not update it with respect to the interval given

<div id="info"><div>

 <script>
 function update(){
                $.getJSON("http://track.gvhub.co/api/livetrack", function (data) {
                    $.each(data, function (key, value) {
                        var lat = parseFloat(data.lat)
                        var lon = parseFloat(data.lon)

                        $("#info").append(key + ":" + value + "<br>");
                        console.log(data.lat, data.lon)
                    })
                })
            }
            setInterval(update(), 1000);
    </script>
Sarath Dev
  • 83
  • 1
  • 1
  • 9

1 Answers1

1

You need to pass a callback to setInterval. Technically you are calling immediately and passing the the return value of update() as a callback. Just remove the ().

See the setInterval(func, delay, [arg1, arg2, ...]) documentation's Parameters section:

func: A function to be executed every delay milliseconds. The function is not passed any arguments, and no return value is expected.

You can do that like:

setInterval(update, 1000);

I hope that helps!

norbitrial
  • 14,716
  • 7
  • 32
  • 59