RandomQuoteMachine - how to refresh API data

RandomQuoteMachine - how to refresh API data
0

#1

i have a trouble figuring out how to refresh API data … any suggestions?

thank you ! :slight_smile:


#2

Use .html() instead of .append(), otherwise each new quote is appended on the screen.

If you have a problem getting different data each time (you get a result from your cache) then use:

$.ajaxSetup({
    cache:false
});

#3
Another interesting way is to set timer - interval in which you can
reload data :slight_smile:

$(document).ready(function() {
  function getQuote(){
      setInterval(function(){
          $.getJSON("http://api.forismatic.com/api/1.0/?method=getQuote&lang=en&format=jsonp&jsonp=?", 
           function(json) {
                $("h3").html(json.quoteText);
                $("h4").html("Author: " + json.quoteAuthor);
           }, "json");
    }, 3000);
  }
  $("button").on('click', function() {
     getQuote();
  }); 
});

It will set timer to 3000 ms = 3 seconds which will then refresh your data too.


#4

I dont know how long this has bothered me, until you came my way dear Sir. THank you!!!