I have this jQuery function:
$(document).ready(function() {
var timeVal = parseInt($('#timeMin').text());
var mins = 60;
$("#startbtn").click(function() {
setInterval(time, 1000)
});
function time() {
if (timeVal >= 0) {
if (mins > 0) {
if (mins <= 10) {
console.log(mins - 1);
$("#clock").html(timeVal + " : 0" + (mins - 1));
mins -= 1;
} else {
console.log(mins);
$("#clock").html(timeVal + " : " + (mins - 1));
mins -= 1;
}
} else if (mins === 0) {
timeVal -= 1;
mins = 60;
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="timeMin">
1
</div>
<button type="button" id="startbtn">
start
</button>
<br><br>
<div id="clock">
00:00
</div>
So it's supposed to execute every second once I click the button ('startbtn'). It's working fine. It goes to 00:00. However when I click it a second time it goes faster. Why does it do that.
PS. Here's the fiddle :
Thank you for your answers.