Skip to content

Instantly share code, notes, and snippets.

@Glutexo
Created September 24, 2020 09:09
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 Glutexo/711e26de0b465641de70f1d2fc91ea69 to your computer and use it in GitHub Desktop.
Save Glutexo/711e26de0b465641de70f1d2fc91ea69 to your computer and use it in GitHub Desktop.
A simple experiment with JavaScript objects
<!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