Skip to content

Instantly share code, notes, and snippets.

@MarkoSh
Last active June 16, 2025 21:29
Show Gist options
  • Save MarkoSh/c91e62fb276f8393113a83ebafb53fb4 to your computer and use it in GitHub Desktop.
Save MarkoSh/c91e62fb276f8393113a83ebafb53fb4 to your computer and use it in GitHub Desktop.
class WorkerInterval {
worker = null;
constructor(callback, interval) {
const $this = this;
const blob = new Blob([`setInterval(() => postMessage(0), ${interval});`]);
const workerScript = URL.createObjectURL(blob);
$this.worker = new Worker(workerScript);
$this.worker.onmessage = callback;
}
stop() {
const $this = this;
$this.worker.terminate();
}
}
const interval = new WorkerInterval(callback, 100);
// sometime later
interval.stop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment