Bonjour à tous,

Je souhaite modifier la fin du compte à rebours de ce pendule d'échecs :

lorsque l'une des deux horloges arrive à 0, l'autre poursuive son décompte jusqu'à 0 au lieu de s'arrêter.

L'utilisation de ce pendule modifié est pour un jeu de société prototype.

Je vous remercie de votre aide.

Source du script JS avec éditeur pour modification : https://codepen.io/mladenilic/pen/reozVV

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class ChessClock {
  static WHITE = 0;
  static BLACK = 1;
  constructor(time, increment, on_update) {
    this.times = [0, 0];
    this.settings = {
      time: time || 0,
      intervalTime: 10,
      increment: increment || 0,
    };
 
    this.turn = ChessClock.WHITE;
    this.interval = null;
    this.on_update = on_update;
 
    this.reset();
  }
 
  static formatTime(timestamp) {
    let min = (timestamp / 1000 / 60) << 0;
    let sec = ((timestamp / 1000) % 60) << 0;
 
    return min + ':' + ('0' + sec).slice(-2);
  }
 
  updateTime () {
    this.times[this.turn] = this.times[this.turn] - this.settings.intervalTime;
 
    if (this.times[this.turn] <= 0) {
      this.pause();
    }
 
    this.update();
  }
 
  start() {
    if ( this.interval) {
      return;
    }
 
    this.interval = setInterval(this.updateTime.bind(this), this.settings.intervalTime);
  }
 
  pause() {
    if (! this.interval) {
      return;
    }     
 
    clearInterval(this.interval);
    this.interval = null;
  }
 
  paused() {
    return this.interval === null;
  }
 
  move() {
    this.times[this.turn] = this.times[this.turn] + this.settings.increment;
 
    this.turn = !this.turn * 1;
 
    this.update();
  }
 
  update() {
    this.on_update && this.on_update(
      ChessClock.formatTime(this.times[ChessClock.WHITE]),
      ChessClock.formatTime(this.times[ChessClock.BLACK]),
    );
  }
 
  reset() {
    this.times[ChessClock.WHITE] = this.settings.time;
    this.times[ChessClock.BLACK] = this.settings.time;
 
    this.turn = ChessClock.WHITE;
 
    this.update();
  }
 
  isWhiteTurn() {
    return ChessClock.WHITE === this.turn;
  }
 
  isBlackTurn = function () {
    return ChessClock.BLACK === this.turn;
  }
}
 
document.addEventListener('DOMContentLoaded', () => {
  const white_field = document.getElementById('white-field');
  const black_field = document.getElementById('black-field');
  const white_timer = document.getElementById('white-time');
  const black_timer = document.getElementById('black-time');
  const play_button = document.getElementById('play');
 
  const clock = new ChessClock(1 * 60 * 1000, 0, (white, black) => {
    white_timer.innerHTML = white;
    black_timer.innerHTML = black;
  });
 
  play_button.addEventListener('click', (e) => {
    e.preventDefault();
 
    play_button.classList.toggle('pause');
 
    black_field.classList.remove('active');
    white_field.classList.remove('active');
 
    if (clock.paused()) {
      white_field.classList.add('active');
 
      clock.reset();
      clock.start();
    } else {
      clock.pause();
    }
  });
 
  white_field.addEventListener('click', (e) => {
    e.preventDefault();
 
    if (!clock.paused() && clock.isWhiteTurn()) {
      white_field.classList.remove('active');
      black_field.classList.add('active');
      clock.move();
    }
  });
 
  black_field.addEventListener('click', (e) => {
    e.preventDefault();
 
    if (!clock.paused() && clock.isBlackTurn()) {
      black_field.classList.remove('active');
      white_field.classList.add('active');
      clock.move();
    }
  });
 
  play_button.click();
});