Skip to content

Instantly share code, notes, and snippets.

@joakin
Created May 22, 2021 18:12
Show Gist options
  • Save joakin/ac87307a25ad890b99c1c7328959d91e to your computer and use it in GitHub Desktop.
Save joakin/ac87307a25ad890b99c1c7328959d91e to your computer and use it in GitHub Desktop.
Countdown minutes and write them to a file, useful for OBS or other programs that want to read files.
let fs = require("fs");
let [, , time] = process.argv;
if (!time) throw new Error("Define a countdown in minutes");
let minutes = Number(time);
if (!minutes || minutes <= 0)
throw new Error(`${time} parsed as ${minutes} which is not a valid time");`);
let timeRemaining = minutes * 60 * 1000;
let lastUpdate = Date.now();
let interval = setInterval(() => {
let now = Date.now();
let dt = now - lastUpdate;
lastUpdate = now;
timeRemaining = timeRemaining - dt;
let minutes = Math.floor(timeRemaining / 1000 / 60);
let seconds = Math.floor(timeRemaining / 1000) % 60;
let timeStr =
String(minutes).padStart(2, "0") + ":" + String(seconds).padStart(2, "0");
fs.writeFileSync("./countdown.txt", timeStr);
if (minutes === 0 && seconds === 0) {
clearInterval(interval);
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment