Skip to content

Instantly share code, notes, and snippets.

@SiZapPaaiGwat
Forked from manast/interval.js
Last active August 29, 2015 14:19
Show Gist options
  • Save SiZapPaaiGwat/ca6920a4658b3a4babd6 to your computer and use it in GitHub Desktop.
Save SiZapPaaiGwat/ca6920a4658b3a4babd6 to your computer and use it in GitHub Desktop.
add ES6 support
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