2
Let’s imagine the following code:
function FazAlgoTrabalhoso(parametro1, parametro2, parametro3){
//Algo trabalhoso aqui
}
var i;
for(i=0;i<=coisas.length;i++){
//"coisas" é um array já preenchido
FazAlgoTrabalhoso(coisas[i].algo1, coisas[i].algo2, coisas[i].algo3);
}
$( "#botao_para_cancelar" ).click(function() {
//Algo para cancelar...
});
- Let’s say the "things" array has 100 positions, so the loop for goes run 100 times, ie will make "something laborious" 100 times.
- Let’s also consider that "Fazalgotrabalhoso" takes about 5 seconds to fully execute.
My question is: How to manage the cancellation of "Fazalgotrabalhoso"? For example, if it has run 50 times, how to cancel subsequent runs via a button? I did not succeed in my attempts and it always ends up running the whole loop....
if ( condition ) Return
– Reginaldo Rigo
I think the problem is in the loop for. Even if you have a Return inside the function, the for has already "ordered" the 100 runs and it ends up passing...
– Pedro Antônio