Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to show 2 buttons for timer of 3 minutes, but the problem is that my second timer works perfectly but first timer doesn't, even if I remove the second timer still there is some problem with the first timer. Code is same for both the timer just variables are different.

What I have tried:

HTML
<table>
<tr class="odd">
           <td></td>
            <th scope="row"><div class="timer">
            <time id="countdown"></time>
            </div>
            </th>
            <td></td>
            <td><form name="form"><input type="button" name="start" id="colourButton" value="Start Time!" onclick="showDiv()"/></form></td>
          </tr>
<tr class="odd">
           <td></td>
            <th scope="row"><div class="timer">
            <time id="countdown2"></time>
            </div>
            </th>
            <td></td>
            <td><form name="form1"><input type="button" name="start1" id="colourButton2" value="Start Time!" onclick="showDiv2()"/></form></td>
          </tr>
</table>



JavaScript
<script>
function showDiv() {
    document.form.colourButton.disabled = true;
   var countdownTimer = setInterval('secondPassed()', 1000);
}
 var seconds = 180;
      function secondPassed() {
          var minutes = Math.round((seconds - 30)/60),
              remainingSeconds = seconds % 60;

          if (remainingSeconds < 10) {
              remainingSeconds = "0" + remainingSeconds;
          }

          document.getElementById('countdown').innerHTML = minutes + ":" + remainingSeconds;
          if (seconds == 0) {
              clearInterval(countdownTimer);
             //form1 is your form name
            window.alert("Times Up");
          } else {
              seconds--;
          }
      }


       function showDiv2() {
    document.form1.colourButton2.disabled = true;
   var countdownTimer1 = setInterval('secondPassed1()', 1000);
}
 var seconds1 = 180;
      function secondPassed1() {
          var minutes1 = Math.round((seconds1 - 30)/60),
              remainingSeconds1 = seconds1 % 60;

          if (remainingSeconds1 < 10) {
              remainingSeconds1 = "0" + remainingSeconds1;
          }

          document.getElementById('countdown2').innerHTML = minutes1 + ":" + remainingSeconds1;
          if (seconds1 == 0) {
              clearInterval(countdownTimer1);
             //form1 is your form name
            window.alert("Times Up");
          } else {
              seconds1--;
          }
      }
      </script>
Posted
Updated 13-Dec-16 8:44am

1 solution

Neither of your clearInterval calls will work - the variable you're passing as the parameter is defined in a different function.

Other than that, your code works as expected: Demo[^]

If you still can't get it working, you'll need to explain what the problem is.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900