-1

How would I re-run this jQuery code every second please?

$.getJSON('http://example.com', function(data) {
        console.log(data);
});

I have done some research and it said I needed to put it into a function and set an interval, however I am not sure how to do this.

Mr. PV
  • 73
  • 1
  • 7
  • Possible duplicate of [setTimeout or setInterval?](https://stackoverflow.com/questions/729921/settimeout-or-setinterval) – Michelangelo Nov 26 '17 at 10:05
  • You can learn about it at [MDN: `setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval). – str Nov 26 '17 at 10:06
  • 2
    I'm voting to close this question as off-topic because a little research and trying is required to ask a question here – Jonas Wilms Nov 26 '17 at 10:10

1 Answers1

1

You should use setInterval() which takes function and the interval duration in milliseconds. In your case it should 1000 milliseconds which indicates 1 second.

setInterval(function() {
  $.getJSON('http://example.com', function(data) {
    console.log(data);
  });
}, 1000);
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35