JSFiddle - React, Tailwind, and code Playground
JavaScript
//generate a recurssive reoccuring ajax request based off a scenario returned from an API
//This is the scenario data for now
// var scenario = {
// "base": {
// "frequency": "5000"
// },
// "endpoints": [
// {
// "method": "GET",
// "type": "JSON",
// "endPoint": "https://api.github.com/users/alvarengarichard",
// "queryParams": {
// "objectives": "objective1, objective2, objective3"
// }
// }
// ]
// }
//Function to Call API
function callApi(method, type, endpoint, onComplete) {
//if there's no oncomplete method...don't do anything on complete
if(typeof onComplete === undefined) {
onComplete = null;
}
//request parameters
var $request = jQuery.ajax({
type: method,
dataType: type,
url: endpoint,
// headers: headers,
// data: params,
complete: onComplete
});
//return actual request
return $request;
}
//Setup poll function, self executing...uses stuff from the scenario
function poll(method, type, endpoint, frequency, onComplete) {
setTimeout(function () {
callApi(method, type, endpoint, onComplete);
console.log("call api");
}, frequency);
}
//Setup first request to get the actual scenario data, and then kick off the polling
function getScenario() {
var method = "GET",
type = "JSON",
endpoint = "http://demo3858327.mockable.io/scenario";
//call the mockserver, and get the scenario...then run the setupPoll function
callApi(method, type, endpoint, setupPoll);
}
//setup poll using response from mock server
function setupPoll(data){
var scenario = JSON.parse(data.responseText);
console.log(scenario);
method = scenario.endpoints[0].method,
type = scenario.endpoints[0].type,
endpoint = scenario.endpoints[0].endPoint,
//headers = "thing",
//data =...