Skip to content

Instantly share code, notes, and snippets.

@mmckegg
Created May 24, 2016 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmckegg/7cd34fe283d0b15a2aeb988fe4ccdde2 to your computer and use it in GitHub Desktop.
Save mmckegg/7cd34fe283d0b15a2aeb988fe4ccdde2 to your computer and use it in GitHub Desktop.
var watch = require('observ/watch')
module.exports = function (tempo, listener) {
var pos = null
var length = 25
var destroyed = false
var releases = []
if (typeof tempo === 'function') {
releases.push(watch(tempo, setTempo))
} else if (typeof tempo === 'number') {
setTempo(tempo)
}
tick()
return function () {
destroyed = true
while (releases.length) {
releases.pop()()
}
}
// scoped
function setTempo (value) {
length = 60000 / (value * 24)
}
function tick () {
if (destroyed) return false
if (!pos) {
pos = window.performance.now()
}
pos += length
var diff = pos - window.performance.now()
listener()
setTimeout(tick, diff)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment