From 28e68ed74aa94fdf5e33b0c4f7f4406ace0f1121 Mon Sep 17 00:00:00 2001 From: programarivm Date: Thu, 14 Mar 2024 17:08:11 +0100 Subject: [PATCH] Implemented count() --- examples/timer-table.js | 4 +++- src/TimerTable.js | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/examples/timer-table.js b/examples/timer-table.js index 26fb1b6..7e7dbac 100644 --- a/examples/timer-table.js +++ b/examples/timer-table.js @@ -23,4 +23,6 @@ const timerTable = new TimerTable( } ); -timerTable.domElem(); +setInterval(() => { + timerTable.count().domElem(); +}, 1000); diff --git a/src/TimerTable.js b/src/TimerTable.js index b12b5a0..68b18d0 100644 --- a/src/TimerTable.js +++ b/src/TimerTable.js @@ -1,11 +1,11 @@ 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}`; } @@ -13,6 +13,20 @@ export class TimerTable extends AbstractComponent { 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'); @@ -25,6 +39,7 @@ export class TimerTable extends AbstractComponent { bTd.appendChild(bText); tr.appendChild(wTd); tr.appendChild(bTd); + this._el.appendChild(tr); } }