0

I have a simple countdown that is activated by a button. Using onclick i start the action, but I noticed that if you keep pressing the button it increases the time and the countdown is out of sync. How would I go about preventing this from happening, and only allowing one click at a time?

 var count = 10, startcounter = document.getElementById("stercount"); startcounter.onclick = function() { countdown = setInterval(function(){ document.getElementById("countdown").innerHTML = count + " seconds remaining!"; if (count <= 0) { clearInterval(countdown); document.getElementById("countdown").innerHTML = "Done!"; } count--; }, 1000); }; 

DEMO: http://jsfiddle.net/StWXk/1/

4 Answers 4

4

Set a flag variable isClicked default as false. When you click on the button, set isClicked to true. During click event, if isClicked is true then make it return false.

var isClicked = false; var count = 10, startcounter = document.getElementById("stercount"); startcounter.onclick = function() { if(isClicked) return false; isClicked = true; countdown = setInterval(function(){ document.getElementById("countdown").innerHTML = count + " seconds remaining!"; if (count <= 0) { clearInterval(countdown); document.getElementById("countdown").innerHTML = "Done!"; } count--; }, 1000); };​ 

Demo: http://jsfiddle.net/StWXk/2/

Sign up to request clarification or add additional context in comments.

Comments

2

Remove the click handler:

startcounter.onclick = function() { this.onclick = null; ... }; 

http://jsfiddle.net/StWXk/3/

Edit: Based on your pause button requirement, you can re-bind the click handler when the timer is paused:

function startTimer() { this.onclick = null; ... } startcounter.onclick = startTimer; pausecounter.onclick = function() { startcounter.onclick = startTimer; clearTimeout(countdown); }; 

http://jsfiddle.net/StWXk/8/

2 Comments

i notice if i add a stop and pause the start no longer works. see jsfiddle jsfiddle.net/StWXk/7
@mr10 - Name your function and re-bind the click handler when the timer is paused.
1

There are two solutions to this, one is defining and setting a flag to keep track of the timer is running outside of the onclick, the other is calling clearInterval before the countdown to stop any already running countdowns.

Solution 1: A flag to indicate if the timer is running or not.

var count = 10, startcounter = document.getElementById("stercount"), running; startcounter.onclick = function() { if (running) return; running = true; countdown = setInterval(function(){ document.getElementById("countdown").innerHTML = count + " seconds remaining!"; if (count <= 0) { clearInterval(countdown); document.getElementById("countdown").innerHTML = "Done!"; } count--; }, 1000); }; 

Solution 2: Stopping the timer with clearInterval before starting it.

var count = 10, startcounter = document.getElementById("stercount"); startcounter.onclick = function() { clearInterval(countdown); countdown = setInterval(function(){ document.getElementById("countdown").innerHTML = count + " seconds remaining!"; if (count <= 0) { clearInterval(countdown); document.getElementById("countdown").innerHTML = "Done!"; } count--; }, 1000); }; 

Solution 3: You can use countdown as your flag, but when doing so you must make sure to set it to null every time you clear it. This is basically the same as solution 1 except for dropping that one var.

Edit: This might not actually work if you don't define countdown in your vars list above the onclick since your countdown-DIV will populate the countdown-global.

var count = 10, startcounter = document.getElementById("stercount"); startcounter.onclick = function() { if (countdown) return; countdown = setInterval(function(){ document.getElementById("countdown").innerHTML = count + " seconds remaining!"; if (count <= 0) { clearInterval(countdown); countdown = null; document.getElementById("countdown").innerHTML = "Done!"; } count--; }, 1000); }; 

ps. Did you forget to define countdown in the vars at the top of the code or is countdown defined elsewhere? Right now it will be a global and any other piece of code (e.g. a library) could overwrite it.

Comments

0

My solution is pretty easy using .ONE, after timeout (2seconds) bind .ONE() again to button

<script> var execute_form_func = function(){ console.log("Click on button"); //some code or other stuff .... // bind same function to button setTimeout(function(){ console.log("Enabling"); $( "#update_submit" ).one("click",execute_form_func); }, 2000); }; $(document).ready(function(){ $( "#update_submit" ).one("click",execute_form_func); }); </script> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.