Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The behavior of clearInterval and clearTimeout differs from what is written on MDN #437

Closed
luccasr73 opened this issue May 9, 2024 · 1 comment

Comments

@luccasr73
Copy link

luccasr73 commented May 9, 2024

First of all, thank you for this incredible library! timers in web workers are wonderful and much more reliable

MDN says Passing an invalid ID to clearTimeout() silently does nothing; no exception is thrown but workers-timers throw an error when i pass a invalid id to clearInterval or clearTimeout and this error cannot caught by try-catch, this has caused me some strange infinite loop problems

repro: https://stackblitz.com/edit/vitejs-vite-sg1zde?file=src%2FApp.tsx

@chrisguttandin
Copy link
Owner

Hi @luccasr73, thanks for reporting this. When looking into it I realized that this behavior even caused a memory on the worker. I aligned the behavior with the one of the native timers. I published the changes as v8.0.0.

The only remaining difference should be that intervals and timeouts are handled separately.

worker-timers/README.md

Lines 45 to 59 in 10fa538

## Differentiation between Intervals and Timeouts
The native WindowTimers only maintain a single list of timers. But `worker-timers` maintains two separate lists to store the ids of intervals and timeouts internally. WindowTimers allows intervals to be cancelled by calling `clearTimeout()` and the other way round because it stores all timers in a single list. This is not possible with `worker-timers`.
```js
const periodicWork = () => {};
// This will stop the interval.
const windowId = window.setInterval(periodicWork, 100);
window.clearTimeout(windowId);
// This will not cancel the interval. It may cancel a timeout.
const workerId = setInterval(periodicWork, 100);
clearTimeout(workerId);
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants