Timer

by black strings

HTML

<div>
<button id="btn">Estimate</button>
<p>
Time: <span id="display">0:00</span>
</p>
</div>

JavaScript

//this is a dynamic module
var mod = function(){
	this.init();
};

mod.prototype.init = function(){
	  this.counter = 0;
  	this.timerLimit = 3000;
    //this.interval = null;
}

mod.prototype.estimate = function(){
	this.startEstimateTimer();
}

mod.prototype.display = function(){
	this.counter++;
	$('#display').html("done : " + this.counter);
}

mod.prototype.startEstimateTimer = function(){
		//clearTimeout(timeoutEvent);
    //clearInterval(this.interval);
	var me = this;
  
  //you need to clear the previous interval before you run it
  clearInterval(me.interval);
  
  //set the function to a variable
	this.interval = setInterval(function(){
  	me.display();
    
    //stop the interval in itself so it only does it once
    clearInterval(me.interval);
  }, me.timerLimit);
}

var m = new mod();

$('#btn').on('click',function(){
	m.estimate();
});