Skip to content Skip to sidebar Skip to footer

Live Update Get Request

Using javascript / another client side scripting language I need to show the result of a http get request, and update every X seconds without refreshing. The api I am working with

Solution 1:

I strongly advice to call the function again inside the success of the api call. A solution using setInterval may hammer the site even when it gives errors. Also the request can take longer than 2 second to execute

Here I use jQuery for simplicity's sake

Use setTimeout inside the success:

functiongetIt() { 
  $.get("url",function(data) { 
    $("#container").text(data); 
    setTimeout(getIt,2000);
  });
}
getIt();

If the URL is crossdomain, you may want to look into JSON and CORS: How to get a cross-origin resource sharing (CORS) post request working

Solution 2:

Just use Ajax and setInterval():

setInterval(function(){ 

//your Ajax call goes here 

}, 2000);

Post a Comment for "Live Update Get Request"