I'm getting my response from my GET requests to this API that I'm working with which is great. I just need the responses to be random every time I click my button, but I'm having trouble with that. I have a math.random
function below in my code, but I don't have it active right now because I'm not sure what to do with it. I've tried some different approaches but nothing seems to work.
$(document).ready(function(){
//When you click the submit button it fires off the getRequest.
$(function(){
$('#search-term').submit(function(e){
e.preventDefault();
myFunction();
//clearText();
});
});
// This is the get request. I want it to have a random function that randomizes the callback data. Once you get the randomizes data, it shows in the console window. I want this function display three random results based on the myFunction() below
function getRequest(){
$.ajax({
url:'https://api.foursquare.com/v2/venues/search? ll=40.7,-74&client_id={my client id]&client_secret=[my secret]',
type: 'GET',
dataType:'json',
success: function(data){
var venues = data['response']['venues'];
//This is my random function which I do not know what to do with
var random = [Math.floor(Math.random() * venues.length)];
$.each(venues, function(key,index){
console.log(index.name);
$('#search-results').append(index.name + '<br>');
})
}
})
};
//This is the function that calls getRequest function three times then stops.
function myFunction(){
var myVar = setInterval(function(){getRequest();}, 500);
//clearTimeout(myVar);
setTimeout(function( ) { clearInterval( myVar); }, 1600);
}
//This function clears the text that exists in the form so that the new random text can populate it.
function clearText(){
$("#search-results").text("");
};
});