Skip to content

Instantly share code, notes, and snippets.

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