I have a website and a script with a for loop which uses the variable (i);
So, the loop looks like this:
var i;
for(i=start_n;i<=end_n;i++){
//function to execute
}
So, I need a function which can intercept for loop and stop it immediately when a ‘Stop’ button is clicked.
I tried the following functions:
1st:
$('#btnStop').click(function(){
i=end_n;
});
2nd:
$('#btnStop').click(function(){
stopLoading = true;
});
for(i=start_n;i<=end_n;i++){
if(stopLoading==true){break;}
//function to execute
}
Neither of them worked. So, is there any way to intercept that for loop and stop it?
Have you tried setInterval instead of for loop ?
Ex.: