Files
al-code/js/91.utils_default.js
T
2026-11-12 20:18:08 +01:00

44 lines
1.1 KiB
JavaScript

class ToggleLoop {
constructor({ id, label, fn, interval, top=true, onColor = "#00AA00", offColor = "#AA0000" }) {
this.id = id;
this.label = label;
this.fn = fn;
this.interval = interval;
this.onColor = onColor;
this.offColor = offColor;
this.loopId = null;
this.render();
var btn_fn = add_bottom_button
if ( top ) { btn_fn = add_top_button }
btn_fn(this.id, `${this.label}: AUS`, () => this.toggle());
}
get running() {
return this.loopId !== null;
}
start() {
if (this.running) return;
this.loopId = setInterval(this.fn, this.interval);
this.render();
}
stop() {
if (!this.running) return;
clearInterval(this.loopId);
this.loopId = null;
this.render();
}
toggle() {
this.running ? this.stop() : this.start();
}
render() {
try {
set_button_value(this.id, `${this.label}: ${this.running ? "AN" : "AUS"}`);
set_button_color(this.id, this.running ? this.onColor : this.offColor);
} catch (e) {}
}
}