lines.forEach
expects to be passed a function which will be called for every item in the
lines
collection.
You are passing in the value returned from the
setTimeout
function, which is an integer. This cannot be used as a callback function parameter.
Your question says you want to call a function every five seconds. But
setTimeout
will only call a function
once, after the specified delay.
I suspect you want something more like this:
$(function(){
started = true;
const interval = 5000;
let abortController = new AbortController();
const checkLine = function(value){
const ajaxCall = $.ajax({
url: url,
type: "GET",
data: someData,
success: function(data){
abortController.signal.throwIfAborted();
doneCheck();
setTimeout(function() { checkLine(value) }, interval);
}
});
abortController.signal.addEventListener("abort", function(){
ajaxCall.abort();
});
};
const total = line.length;
line.forEach(checkLine);
$("#stop").click(function(){
abortController.abort();
$("#start").property("disabled", false);
$("#stop").property("disabled", true);
});
$("#start").click(function(){
abortController = new AbortController();
$("#start").property("disabled", true);
$("#stop").property("disabled", false);
line.forEach(checkLine);
});
});