Created
February 20, 2016 11:05
-
-
Save innermond/655743152f36a8af73aa to your computer and use it in GitHub Desktop.
Give heart beat capacity to a function, execute it at regular interval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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