I am trying to break the setTimeout function which is started on page load. So what I am doing here is, If I click on the button then I making flag value to true and setTimeout should break which isn't happening here. This setTimeout function is inside the for each loop. Below is my code.
rData[0].dt.forEach(function(d, i) {
setTimeout(function() {
if(flag === "false"){
console.log(flag);
reserRadius(i); //here I am changing radius of circle
}else{
console.log(flag);
clearTimeout();
return;
}
}, i * 2000);
});
clearTimeoutexpects an id returned fromsetTimeout, but where you have it doesn't make sense, because by the time the function is evaluated the timeout is already completed and there is no reason to callclearTimeouttimeoutinside itself, because when executed, it is already removed. It will be useful to post a JSFiddle snippet in your question.