AJAX request via Giphy API

by karim79

JavaScript

function beDaniel(searchTerm) {
    console.log('searchTerm is: ', searchTerm)
    $.ajax({
      url: "//api.giphy.com/v1/gifs/search?q=" + searchTerm + "&api_key=dc6zaTOxFJmzC",
      type: "GET",
      success: function(response) {
        console.log(response)
        var max = response.data.length - 1 //length of response, minus 1 (cuz array starts at index 0 innit)
        console.log('response.data.length is ', max)
        //var randomNumber = Math.round(Math.random() * max) //random number between 0 and max -1
        var randomNumber = Math.round(Math.random() * 6) //random number between 0 and max -1
        // get Danielonian neuronal output. Put in ceiling of 20 because not much going on
        var gifUrl = "https://media.giphy.com/media/" + response.data[Math.floor(Math.random() * Math.floor(20))].id + "/giphy.gif"
        console.log(gifUrl);
        var body = document.getElementsByTagName('body')[0];
        body.style.backgroundImage = `url(${gifUrl})`;
      },
      error: function(e) {
        alert(e);
      }
    });
  }
  
  var DanielDNA = [ "Lenovo", "Red Shoes", "The Witcher" ];
  
  // begin highly accurate simulation of life form known as Daniel
  setInterval(function() {
  	beDaniel(DanielDNA[Math.floor(Math.random() * DanielDNA.length)])
  }, 3000);