How to invoke function on the first click of the button and another on the second click

0

How do I make the first function to be called within the first click of the button, by the event ondblclick(); and the second function in the second click of the same button.

I imagine invoking the function of the first click on the button and another on the second click, like this:

Notice how it should be:

function a(){
   ...
declaração 
   ...
}
function b(){
   ...
declaração
   ...
}

Already on the button, something like:

<button ondblclick="a();?:b();">2-in-1</button>

It is simple for those who know how to manipulate ?: , the same as if .. else , but with a small and appropriate formula for this idea.

Thank you in advance, everyone's help.

    
asked by anonymous 19.05.2016 / 07:57

1 answer

0

Dude, I do not know if this is the best practice, but it worked for me.

JQuery

var cont = 0;
$( "#teste" ).click(function() {  
    if(cont == 0)
      {
     a();
      cont += 1;
      }
  else
      {
      b();
      cont = 0;
      }
});

function a(){
 alert('aqui');
}

function b(){
 alert('aqui 2');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><ahref="#" id="teste">
teste
</a>

Now transcript to:

JavaScript

var cont = 0;

document.getElementById( "teste" ).onclick = function() {  
if(cont == 0)
{
a();
cont += 1;
}
else
{
b();
cont = 0;
}
}

function a(){
alert('função A');
}

function b(){
alert('função B');
}
<button id="teste">Clique-e-Descomplique</button>
    
19.05.2016 / 18:39