When I click the button more than 1 time the count accelerates

0

People I got from a friend here of the forum a script of a button with countdown, that upon clicking if the count begins. Thank you very much because it is very good ... there is only one problem, when I click more than once on the button the countdown accelerates, see the code ↓

<script language="JavaScript">
  function dispara( span ) {
    conta( this, document.getElementById( span ) );
  }

  function conta( botao, contador ) {
    botao.disabled=true;
    if(contador.innerHTML==0) {
      contador.innerHTML = 10;
      botao.disabled=false;
      return false;
    }
    contador.innerHTML = contador.innerHTML - 1;
    setTimeout( function(){conta( botao, contador )}, 1000 );
  }
</script>

<input type="button" value="botao1" onclick="dispara('s1')" class="btn">
<span id="s1">10</span>
<br/><br/>
<input type="button" value="botao2" onclick="dispara('s2')" class="btn">
<span id="s2">10</span>

Could you help me solve this problem?

    
asked by anonymous 30.06.2016 / 22:25

1 answer

1

Well, I decided that way. I know it's not the best way, but it's a solution. As the colleague said, he is not taking the button with this. Then I put the button id, it is passed to the function, and this button id put the same name as the counter class, so what it passes to the function is the button id and also the counter class, so I can find the 2. Look:

    <script language="JavaScript">
  function dispara( span ) {
    conta( span, document.getElementsByClassName( span )[0]);
  }

  function conta( botao, contador ) {

    document.getElementById(botao).disabled=true;
    if(contador.innerHTML==0) {
      contador.innerHTML = 10;
      document.getElementById(botao).disabled=false;

      return false;
    }
      contador.innerHTML = contador.innerHTML - 1;
    setTimeout( function(){conta( botao, contador )}, 1000 );
  }
</script>

<input type="button" value="botao1" onclick="dispara('s1')" id="s1" class="btn">
<span class="s1">10</span>
<br/><br/>
<input type="button" value="botao2" onclick="dispara('s2')" id="s2" class="btn">
<span class="s2">10</span>

I hope I have helped !!!

    
30.06.2016 / 23:31