Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Last active September 8, 2017 16:57
Show Gist options
  • Save Woodsphreaker/8e58e4c6a14c27fdd1c4dfcdddb0bfb9 to your computer and use it in GitHub Desktop.
Save Woodsphreaker/8e58e4c6a14c27fdd1c4dfcdddb0bfb9 to your computer and use it in GitHub Desktop.
intervalClosure
const interval = (id, time = 1000) => {
const el = []
const start = () => {
el[id] = setInterval(() => {
console.log(`${new Date().toLocaleTimeString()} Identificador ${id}\n`)
}, time)
}
const stop = () => {
clearInterval(el[id])
}
return {
start,
stop
}
}
const newIterval1 = interval("id1")
const newIterval2 = interval("id2", 500)
const newIterval3 = interval("id3", 800)
newIterval1.start()
newIterval2.start()
newIterval3.start()
setTimeout(() => newIterval1.stop(), 5000)
setTimeout(() => newIterval2.stop(), 5000)
setTimeout(() => newIterval3.stop(), 10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment