SetInterval 60 second countdown issue jQuery

0 votes

I'm trying to countdown 60 seconds once I click the begin button and at 0 I don't want to just hide the counter, I want it to be reset. I ran into 2 problems: When I click the begin button again it's not starting over. The begin button is not disabled once I click it to start the countdown. While its counting down, when I press the begin button it accelerate the countdown. Any help is appreciated, here is the code:

function begin() {
    $('#begin').prop('disabled');
    myTimer = setInterval(function() {
      $('#timing').html(timing);
      if (timing === 0) {
        alert('Too late! Try again');
        clearInterval(myTimer);
        $('#timing').hide();
      }
      timing--;
    }, 1000);
  }

Jul 21, 2022 in Web Development by gaurav
• 23,260 points
778 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You have several issues:

  1. You need .prop("disabled", true) to disable the button.
  2. You need to reset the timing variable when you restart the timer.
  3. You need to enable the button when the timer finishes so it can be pressed again.
  4. You need to display the initial count when you start the timer

Code:

var timing;
var myTimer;

function begin() {
    timing = 60;
    $('#timing').html(timing);
    $('#begin').prop('disabled', true);
    myTimer = setInterval(function() {
      --timing;
      $('#timing').html(timing);
      if (timing === 0) {
        alert('Too late! Try again');
        clearInterval(myTimer);
        $('#begin').prop('disabled', false);
      }
    }, 1000);
 }
answered Jul 25, 2022 by rajatha
• 7,680 points

edited Mar 5

Related Questions In Web Development

0 votes
0 answers

SetInterval 60 second countdown issue jQuery

I'm trying to countdown 60 seconds once ...READ MORE

Jul 22, 2022 in Web Development by gaurav
• 23,260 points
1,083 views
0 votes
0 answers

How Can I create A 5 second Countdown timer with jquery that ends with a login popup?

How would i create a jquery timer ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
752 views
0 votes
1 answer

$("#select option:second").val() jquery - keep select second value of select box option

Yes, you want the special jQuery eq() selector. ...READ MORE

answered Jun 22, 2022 in Web Development by rajatha
• 7,680 points
3,793 views
0 votes
0 answers

Countdown till midnight in Jquery

I have a simple question, that is ...READ MORE

Jul 25, 2022 in Web Development by gaurav
• 23,260 points
596 views
0 votes
0 answers
0 votes
0 answers

jQuery Slide toggle and fade in issue

As you can see when clicking on ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
843 views
0 votes
1 answer

What is jQuery?

Hey, jQuery is a fast and concise JavaScript ...READ MORE

answered Feb 14, 2020 in JQuery by kartik
• 37,520 points
1,581 views
0 votes
1 answer

Uncaught TypeError: Cannot read property 'msie' of undefined - jQuery tools

Hello, Use the following script tag in your ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
16,620 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,840 points
23,894 views
0 votes
1 answer

How to make Bootstrap popover Appear/Disappear on hover instead of click?

Hello @kartik, Set the trigger option of the popover to hover instead ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,840 points
3,592 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP