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.