Created
September 24, 2020 09:09
-
-
Save Glutexo/711e26de0b465641de70f1d2fc91ea69 to your computer and use it in GitHub Desktop.
A simple experiment with JavaScript objects
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>Pomodoro</title> | |
<meta charset="utf-8"/> | |
</head> | |
<body> | |
<script> | |
(function() { | |
function Pomodoro(seconds) { | |
if (typeof seconds !== "undefined") { | |
this.seconds = seconds | |
} else { | |
this.seconds = this.DEFAULT_SECONDS; | |
} | |
} | |
Pomodoro.prototype.DEFAULT_SECONDS = 60; | |
Pomodoro.prototype.TICK_MS = 1000; | |
Pomodoro.prototype.tick = function() { | |
this.seconds--; | |
} | |
Pomodoro.prototype.start = function(callback) { | |
const self = this; | |
function _tick() { | |
self.tick() | |
if (typeof callback !== "undefined") { | |
callback(self); | |
} | |
} | |
this._interval = setInterval(_tick, this.TICK_MS); | |
} | |
Pomodoro.prototype.stop = function() { | |
clearInterval(this._interval); | |
delete this._interval; | |
} | |
function main() { | |
function _tick(tmr) { | |
console.log(tmr.seconds); | |
if (tmr.seconds <= 100) { | |
tmr.stop() | |
} | |
} | |
const timer = new Pomodoro(120); | |
timer.start(_tick); | |
} | |
main(); | |
})(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment