JSFiddle - React, Tailwind, and code Playground
by joshmoto
JavaScript
// your ajax call interval
let ajax_call_interval = 30000;
// your processed set object for storing processed names
let processed = new Set();
// ajax call running check var
let ajax_call_running = false;
// your ajax call function
function ajax_call() {
// set ajax_call_running to true
ajax_call_running = true;
// jquery ajax call
$.ajax({
type: "GET",
url: "test.php",
dataType: "JSON",
success: function(response) {
// great success
console.log("success");
// loop through response json as key / data
$.each(response, function(key, data) {
// if processed set object has data.name already
if (processed.has(data.name)) {
// log name already exists
console.log("name already exists in processed");
// this will skip to the next json data item in each loop
// else if name does exist in processed set object then...
} else {
// add new name to processed set object var for use in next ajax call
processed.add(data.name);
// log name added to processed
console.log("new name added to processed");
// do your other magic here for unprocessed name json data
// ...
let name = data.name;
let size = data.size;
}
});
// i am assuming the below should run once the above jquery each function has completed looping through your json response
// set ajax_call_running to true
ajax_call_running = false;
// re-run our ajax call in 30000 ms
ajax_call_runner(ajax_call_interval);
},
error: function(error) {
// oh no
console.log("error");
// do error response stuff here...
// set ajax_call_running to true
ajax_call_running = false;
// re-run our ajax call in 30000 ms anyway
ajax_call_runner(ajax_call_interval);
}
});
}
// your ajax call function runner
function...