Using JSON together with JS functions?

Using JSON together with JS functions?
0.0 0

#1

I am creating a random quote generator, except there is a delay when I click the “get quote” button. My idea is to preload 3 quotes before the $(‘document’).ready and put them into an array, then later call the random quote from this array.

I am trying to make an array made up of data I get from JSON API. I can’t get the global variable to be recognized inside of the function, then I can’t seem to return anything on the function. can someone please point me in the right direction?

var quotes = [];
function getQuotes() {
  
  for (var i = 0; i < 2; i++) {
    $.getJSON(
      "https://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=",
      function(data) {
        quotes.push(data[0].content);
      }
    );
  }
return quotes;    
}

#3

interesting! Thanks for the response, it was exactly what I was looking for. so just to clarify, how can I use something from inside a function that calls an API, outside the function? I would just return the variable that I want to use later?