-
-
Save SiZapPaaiGwat/ca6920a4658b3a4babd6 to your computer and use it in GitHub Desktop.
add ES6 support
This file contains 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
export default function timer(fn, duration) { | |
this.baseline = 0 | |
this.run = function(exec){ | |
if(!this.baseline){ | |
this.baseline = Date.now() | |
} | |
exec && fn() | |
this.baseline += duration | |
var end = Date.now() | |
var nextTick = duration - (end - this.baseline) | |
if(nextTick < 0) { | |
nextTick = 0 | |
} | |
var self = this | |
this.timer = setTimeout(() => self.run(true), nextTick) | |
} | |
this.stop = function(){ | |
this.baseline = 0 | |
clearTimeout(this.timer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment