I need to call an API in a delay of 1 second between each call. I have this code in which the timeout doesn't work:
for(var i=0; i < contentSize; i++){
var content = contentArray[i];
var j = table[i];
var thisColumn = document.getElementById(j);
if(content.includes('dl-media')){//content is image
setTimeout(function() {evaluateImage(content, thisColumn); }, 2000);
}
else if(content != ""){//content is text
// evalutaeText(language, content, thisColumn);
}
else{
$(thisColumn).replaceWith("<div>No content</div>");
}
}
Only if I set the timeout on the for loop then there is a delay of 2 seconds between one call to another, but then it takes too long and I only need the delay on the function call. The function that I'm calling executes an Ajax call:
$.ajax({
url: "https://westus.api.cognitive.microsoft.com/contentmoderator/moderate/v1.0/ProcessImage/Evaluate?" + $.param(params),
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","bd43e");
},
type: "POST",
// Request body
data: "{'DataRepresentation':'URL', 'Value':" + content +"}",
})
.done(function(data) {
$(thisColumn).replaceWith("<div id="+thisColumn.value+">" + data.AdultClassificationScore +"<br>"+ data.RacyClassificationScore + "</div>");
})
.fail(function() {
$(thisColumn).replaceWith("<div id="+thisColumn.value+">Failed</div>");
});