Last active
June 14, 2017 18:45
-
-
Save hoffination/332183febc40387fef6eed3ef3f8acea to your computer and use it in GitHub Desktop.
'this' example for a microwave
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
(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