Skip to content

Instantly share code, notes, and snippets.

@hoffination
Last active June 14, 2017 18:45
Show Gist options
  • Save hoffination/332183febc40387fef6eed3ef3f8acea to your computer and use it in GitHub Desktop.
Save hoffination/332183febc40387fef6eed3ef3f8acea to your computer and use it in GitHub Desktop.
'this' example for a microwave
(function() {
"use strict"
const microwave = {
_timer: null,
_eventListener: null,
start: function(timeMS, callback) {
this._eventListener = callback
this._timer = setTimeout((() => {
this._timer = null
this._eventListener('done')
}).bind(this), timeMS)
},
stop: function() {
if (this._timer) {
clearTimeout(this._timer)
this._timer = null
this._eventListener = null
return true
} else {
return false
}
},
isRunning: function() {
return !!this._timer
}
}
microwave.start(10000, console.log)
console.log(microwave.isRunning()) // true
console.log("stopped: ", microwave.stop()) // stopped: true
console.log(microwave.isRunning()) // false
microwave.start(100, console.log) // .... done
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment