JSFiddle - React, Tailwind, and code Playground
HTML
<div id="timer"></div>
<input type="button" id="knap" value="start" />
<input type="button" id="reset" value="reset" />
JavaScript
$("#knap").click(function start()
{
$(this).unbind('click');
$("#reset").unbind('click').click((function(timer)
{//timer is in scope thanks to closure
return function()
{//resets timer
clearInterval(timer);
timer = null;
$('#knap').click(start);
}
})(setInterval((function(sec)
{
return function()
{
$('#timer').text(sec--);
if (sec === -1)
{
$('#reset').click();//stops interval
$('#reset').unbind('click');//no more need for the event
alert('done');
}//here's the interval counter: 15, passed as argument to closure
}
})(15),1000)));
//set interval returns timer id, passed as argument to closure
});