Last active
June 16, 2025 21:29
-
-
Save MarkoSh/c91e62fb276f8393113a83ebafb53fb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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