Skip to content

Commit

Permalink
Implemented count()
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Mar 14, 2024
1 parent 5ca7fff commit 28e68ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion examples/timer-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ const timerTable = new TimerTable(
}
);

timerTable.domElem();
setInterval(() => {
timerTable.count().domElem();
}, 1000);
17 changes: 16 additions & 1 deletion src/TimerTable.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
import AbstractComponent from '../src/AbstractComponent.js';
import { Pgn } from '../src/index.js';

export class TimerTable extends AbstractComponent {
_convert(count) {
const h = Math.floor(count / (60 * 60)).toString().padStart(2, '0');
const m = Math.floor(count / 60 % 60).toString().padStart(2, '0');
const s = Math.floor(count % 60).toString().padStart(2, '0');

if (h > 0) {
return `${h}:${m}:${s}`;
}

return `${m}:${s}`;
}

count() {
if (this.props.turn === Pgn.symbol.WHITE) {
if (this.props.timer.w > 0) {
this.props.timer.w -= 1;
}
} else {
if (this.props.timer.b > 0) {
this.props.timer.b -= 1;
}
}

return this;
}

domElem() {
this._el.replaceChildren();
const tr = document.createElement('tr');
Expand All @@ -25,6 +39,7 @@ export class TimerTable extends AbstractComponent {
bTd.appendChild(bText);
tr.appendChild(wTd);
tr.appendChild(bTd);

this._el.appendChild(tr);
}
}

0 comments on commit 28e68ed

Please sign in to comment.