Skip to content

Commit

Permalink
⚡ Reduce latency
Browse files Browse the repository at this point in the history
  • Loading branch information
9at8 committed Jul 23, 2018
1 parent 4726c05 commit 08b70b9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import "./styles.css";

const INITIAL_TIME = 10000;
const INCREMENT = 3000;
const INTERVAL = 100;
const INTERVAL = 10;
const PLAYER_1 = "player1";
const PLAYER_2 = "player2";

class App extends React.Component {
state = {
prevTime: null,
[PLAYER_1]: INITIAL_TIME,
[PLAYER_2]: INITIAL_TIME,
currentPlayer: PLAYER_1,
Expand Down Expand Up @@ -56,9 +57,11 @@ class App extends React.Component {
};

decreaseTime = player => {
const current = Date.now();
this.setState(
state => ({
[player]: Math.max(state[player] - INTERVAL, 0)
[player]: Math.max(state[player] - (current - state.prevTime), 0),
prevTime: current
}),
() => {
if (this.state[player] === 0) {
Expand Down Expand Up @@ -88,13 +91,15 @@ class App extends React.Component {
if (oldTimer == null) {
return {
...newState,
prevTime: Date.now(),
timer: this.createTimer()
};
}

clearInterval(oldTimer);
return {
...newState,
prevTime: null,
timer: undefined
};
});
Expand Down

0 comments on commit 08b70b9

Please sign in to comment.