Skip to content

Instantly share code, notes, and snippets.

@ivillamil
Created May 17, 2013 15:13
Show Gist options
  • Save ivillamil/5599725 to your computer and use it in GitHub Desktop.
Save ivillamil/5599725 to your computer and use it in GitHub Desktop.
A CodePen by Iván Villamil. Restart Timeout - A simple snippet that shows how to restart a timeout.
<input type="number" id="delay" placeholder="Seconds" size="8" />
<input type="button" id="start-btn" value="iniciar" />
<input type="button" id="reset-btn" value="resetear" />
<span id="output">0</span>
;(function($){
App = {
init: function() {
this.delay = 8000;
this.cache();
this.events();
},
cache: function() {
this.delayInput = $('#delay');
this.startBtn = $('#start-btn');
this.resetBtn = $('#reset-btn');
this.output = $('#output');
},
events: function() {
this.startBtn.on('click', $.proxy(this.startInterval, this));
this.resetBtn.on('click', $.proxy(this.resetInterval, this));
},
startInterval: function(e){
var $this = this,
time = 1;
if (this.delayInput.val().length > 0)
this.delay = this.delayInput.val() * 1000;
this.timeout = setTimeout(function(){
alert('finished');
},this.delay);
this.interval = setInterval(function(){
if ((time*1000) <= $this.delay)
$this.output.html(($this.delay/1000) - (time++));
},1000);
},
resetInterval: function() {
clearTimeout(this.timeout);
clearInterval(this.interval);
this.startInterval();
}
},
App.init();
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment