Stop a function already started in JQuery

1

The problem is as follows. I have a function called showJsonProducts (); I want it to, when I call another function it stops running, example:

$('.btn').click(function(){
 showJsonProducts.stop(); 
ou 
 showJsonProducts().break();

Is there something like this?

    
asked by anonymous 13.01.2018 / 14:41

1 answer

1

Once loaded into memory, you can not prevent the function from being called (at least I do not know a method).

But you can create a global variable that ignores the content (or part of it) of the function. Set the variable to false and include the content of the function within a if by checking the status of the variable. When you call the other function, change the variable's status to true :

var ativa = false;
function teste(){
   if(!ativa){
      // ... todo o conteúdo da função aqui
      alert("Olá!");
   }
}

function desativa(){
   ativa = true;
}
<input type="button" value="Chamar função" onclick="teste()" />
<input type="button" value="desativar função" onclick="desativa()" />

In this way the function will still be called but will have no effect because it will not enter the if within it. In your case:

$('.btn').click(function(){
    ativa = true;
});

Edit

Another way is to redefine the function:

window.teste = function(){
   return false;
}

Example:

function teste(){
   alert("Olá!");
}

function desativa(){
   window.teste = function(){
      return false;
   }
}
<input type="button" value="Chamar função" onclick="teste()" />
<input type="button" value="desativar função" onclick="desativa()" />
    
13.01.2018 / 15:47