wtf-games-break-timer 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +54 -0
- package/package.json +32 -0
- package/src/index.js +114 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 WTF Games
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# wtf-games-break-timer
|
|
2
|
+
|
|
3
|
+
A small, framework-free JavaScript timer core for browser games, focus sessions, and screen breaks.
|
|
4
|
+
|
|
5
|
+
It powers the free [Game Break Timer on WTF Games](https://wtfgames.io/game-break-timer/), where you can try the browser version without an account or download.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install wtf-games-break-timer
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const { GameBreakTimer, formatTime } = require("wtf-games-break-timer");
|
|
17
|
+
|
|
18
|
+
const timer = new GameBreakTimer({
|
|
19
|
+
minutes: 20,
|
|
20
|
+
onTick: (seconds) => {
|
|
21
|
+
document.querySelector("#countdown").textContent = formatTime(seconds);
|
|
22
|
+
},
|
|
23
|
+
onComplete: () => {
|
|
24
|
+
document.querySelector("#message").textContent = "Time for a break";
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
timer.start();
|
|
29
|
+
// timer.pause();
|
|
30
|
+
// timer.reset();
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The timer uses a wall-clock deadline, so it stays accurate when a browser throttles an inactive tab. No data is collected or sent by this package.
|
|
34
|
+
|
|
35
|
+
## API
|
|
36
|
+
|
|
37
|
+
- `new GameBreakTimer(options)` — create a timer. `minutes` defaults to `20`.
|
|
38
|
+
- `timer.start()` — start or resume the countdown.
|
|
39
|
+
- `timer.pause()` — pause the countdown.
|
|
40
|
+
- `timer.reset()` — return to the initial duration.
|
|
41
|
+
- `timer.setMinutes(minutes)` — change the duration while paused.
|
|
42
|
+
- `timer.remainingSeconds` — current remaining time.
|
|
43
|
+
- `timer.isRunning` — whether the timer is active.
|
|
44
|
+
- `formatTime(seconds)` — format seconds as `mm:ss`.
|
|
45
|
+
|
|
46
|
+
## Development
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm test
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## License
|
|
53
|
+
|
|
54
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wtf-games-break-timer",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A small, framework-free countdown timer core for browser games and focus breaks.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "node --test"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"game",
|
|
17
|
+
"games",
|
|
18
|
+
"timer",
|
|
19
|
+
"break",
|
|
20
|
+
"browser",
|
|
21
|
+
"productivity"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "https://github.com/Asacura/wtf-games-break-timer.git"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/Asacura/wtf-games-break-timer/issues"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://wtfgames.io/game-break-timer/"
|
|
32
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
class GameBreakTimer {
|
|
2
|
+
constructor({
|
|
3
|
+
minutes = 20,
|
|
4
|
+
onTick = () => {},
|
|
5
|
+
onComplete = () => {},
|
|
6
|
+
tickIntervalMs = 250,
|
|
7
|
+
now = () => Date.now(),
|
|
8
|
+
setIntervalFn = setInterval,
|
|
9
|
+
clearIntervalFn = clearInterval
|
|
10
|
+
} = {}) {
|
|
11
|
+
this.onTick = onTick;
|
|
12
|
+
this.onComplete = onComplete;
|
|
13
|
+
this.tickIntervalMs = tickIntervalMs;
|
|
14
|
+
this._now = now;
|
|
15
|
+
this._setInterval = setIntervalFn;
|
|
16
|
+
this._clearInterval = clearIntervalFn;
|
|
17
|
+
this._intervalId = null;
|
|
18
|
+
this._endAt = null;
|
|
19
|
+
|
|
20
|
+
this.setMinutes(minutes);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get isRunning() {
|
|
24
|
+
return this._intervalId !== null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get remainingSeconds() {
|
|
28
|
+
return this._remainingSeconds;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
setMinutes(minutes) {
|
|
32
|
+
if (this.isRunning) {
|
|
33
|
+
throw new Error("Pause the timer before changing its duration");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!Number.isFinite(minutes) || minutes <= 0) {
|
|
37
|
+
throw new TypeError("minutes must be a positive number");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this._initialSeconds = Math.max(1, Math.round(minutes * 60));
|
|
41
|
+
this._remainingSeconds = this._initialSeconds;
|
|
42
|
+
this._emitTick();
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
start() {
|
|
47
|
+
if (this.isRunning) {
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this._endAt = this._now() + this._remainingSeconds * 1000;
|
|
52
|
+
this._intervalId = this._setInterval(() => this._sync(), this.tickIntervalMs);
|
|
53
|
+
this._sync();
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
pause() {
|
|
58
|
+
if (!this.isRunning) {
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
this._sync();
|
|
63
|
+
this._stop();
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
reset() {
|
|
68
|
+
this._stop();
|
|
69
|
+
this._remainingSeconds = this._initialSeconds;
|
|
70
|
+
this._emitTick();
|
|
71
|
+
return this;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_sync() {
|
|
75
|
+
if (!this.isRunning) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const nextSeconds = Math.max(0, Math.ceil((this._endAt - this._now()) / 1000));
|
|
80
|
+
if (nextSeconds !== this._remainingSeconds) {
|
|
81
|
+
this._remainingSeconds = nextSeconds;
|
|
82
|
+
this._emitTick();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (nextSeconds === 0) {
|
|
86
|
+
this._stop();
|
|
87
|
+
this.onComplete(this);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_stop() {
|
|
92
|
+
if (this._intervalId !== null) {
|
|
93
|
+
this._clearInterval(this._intervalId);
|
|
94
|
+
this._intervalId = null;
|
|
95
|
+
}
|
|
96
|
+
this._endAt = null;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_emitTick() {
|
|
100
|
+
this.onTick(this._remainingSeconds, this);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function formatTime(totalSeconds) {
|
|
105
|
+
const seconds = Math.max(0, Math.floor(totalSeconds));
|
|
106
|
+
const minutes = Math.floor(seconds / 60);
|
|
107
|
+
const remainder = seconds % 60;
|
|
108
|
+
return `${String(minutes).padStart(2, "0")}:${String(remainder).padStart(2, "0")}`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
module.exports = {
|
|
112
|
+
GameBreakTimer,
|
|
113
|
+
formatTime
|
|
114
|
+
};
|