0

I want to use the setInterval function to get the data from other website every 10s. But it can't repeat do it every 10s, just run the first time then stop.

setInterval(hypothes("testtest"), 10000);

function hypothes(username){  
   var xhttp = new XMLHttpRequest();
   xhttp.onreadystatechange = function() {
   if (xhttp.readyState == 4 && xhttp.status == 200) {
      document.getElementById("demo").innerHTML = xhttp.responseText;
      var Jsonfile= JSON.parse(xhttp.responseText);
      console.log(Jsonfile.total);
      console.log(username); 
    }
  };
  var url="https://hypothes.is/api/search?user=" + username + "&sort=created&order=asc";
  xhttp.open("GET", url, true);
  xhttp.send();
  alert("test");
  }
AntiGMO
  • 1,535
  • 5
  • 23
  • 38

3 Answers3

1

setInterval(function(){hypothes("testtest")}, 10000);

RaymondM
  • 318
  • 1
  • 10
1

setInterval takes a function. You passed in the result of that hypothes. Try this:

setInterval(function() { hypothes("testtest") }, 10000);
Fabricator
  • 12,722
  • 2
  • 27
  • 40
0

The problem with your code is that you don't actually set your function as a callback, you're calling it right away.

Modify the code where you set the interval as follows:

setInterval(function() { hypothes("testtest"); }, 10000);
weirdan
  • 2,499
  • 23
  • 27