forked from HoloTech/twitch-subathon-timer
Adjust to new timer channel format.
This commit is contained in:
41
timer.js
41
timer.js
@@ -31,13 +31,15 @@ class Timer {
|
||||
var minutes = Math.floor(dur_seconds / 60);
|
||||
var hours = Math.floor(minutes / 60);
|
||||
minutes = minutes - 60 * hours;
|
||||
|
||||
timestr = String(hours).padStart(2, '0') + ':' + String(minutes).padStart(2, '0');
|
||||
// Change rendering mode based on hours or minutes left
|
||||
if (hours > 0) {
|
||||
timestr = String(hours).padStart(2, '0') + ':' + String(minutes).padStart(2, '0');
|
||||
} else {
|
||||
timestr = String(minutes).padStart(2, '0') + ':' + String(seconds).padStart(2, '0');
|
||||
}
|
||||
}
|
||||
|
||||
if (this.running) {
|
||||
this.renderer.update_time(timestr);
|
||||
}
|
||||
this.renderer.update_time(timestr);
|
||||
}
|
||||
|
||||
tick() {
|
||||
@@ -47,7 +49,9 @@ class Timer {
|
||||
return
|
||||
}
|
||||
this.render_time();
|
||||
setTimeout(this.tick.bind(this), 1000);
|
||||
if (this.running) {
|
||||
setTimeout(this.tick.bind(this), 1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,23 +78,13 @@ class TimerRenderer {
|
||||
this.time_element.textContent = timestr;
|
||||
}
|
||||
|
||||
set_break_color () {
|
||||
this.background.style.setProperty('--stage-color', 'var(--break-color)');
|
||||
this.time_element.style.color = "var(--break-color)";
|
||||
}
|
||||
|
||||
set_focus_color () {
|
||||
this.background.style.setProperty('--stage-color', 'var(--focus-color)');
|
||||
this.time_element.style.color = "var(--focus-color)";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
function communicate(websocket) {
|
||||
console.log("Communicating");
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
websocket.send(JSON.stringify({type: "init", channel: "SubTimer", channelid: params.get('channelid')}));
|
||||
websocket.send(JSON.stringify({type: "init", channel: "SubTimer", community: params.get('community')}));
|
||||
|
||||
var renderer = new TimerRenderer();
|
||||
let timer;
|
||||
@@ -111,11 +105,20 @@ function communicate(websocket) {
|
||||
timer = new Timer(
|
||||
renderer,
|
||||
new Date(args.end_at),
|
||||
true
|
||||
args.running
|
||||
)
|
||||
timer.tick();
|
||||
timer.running = args.running;
|
||||
break;
|
||||
case "noTimer":
|
||||
if (timer != null) {
|
||||
timer.renderer.update_time("--:--")
|
||||
timer.destroying = true;
|
||||
}
|
||||
case "endTimer":
|
||||
if (timer != null) {
|
||||
timer.renderer.update_time("00:00")
|
||||
timer.destroying = true;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unsupported method requested: ${event.method}.`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user