Skip to content

Instantly share code, notes, and snippets.

@innermond
Created February 20, 2016 11:05
Show Gist options
  • Select an option

  • Save innermond/655743152f36a8af73aa to your computer and use it in GitHub Desktop.

Select an option

Save innermond/655743152f36a8af73aa to your computer and use it in GitHub Desktop.
Give heart beat capacity to a function, execute it at regular interval
var Heart = function(pulse){
return {
id: null,
periodical: function(beat){
this.id = setInterval(pulse, beat);
console.log('interval ' + this.id);
return this;
},
stop: function(span){
console.log('before timeout ' + this.id);
setTimeout(
(function(){
console.log('timeout ' + this.id);
clearInterval(this.id);
}).bind(this),
span
);
return this;
}
};
};
var Body = function(heart) {
var message = 'message from inside';
function pulse(){
console.log(message);
}
return heart(pulse);
};
Body(Heart).periodical(1000).stop(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment