eclipsesource / eclipsesource/tabris-js
clearTimeout() does not always work
- Dominant language
- JavaScript
- Stars
- 1.4k
- Forks
- 171
- PR merge metrics
- No merged PRs in 30d
Description
### Problem description
When calling `clearTimeout()` asynchronously, it does not always cancel timeouts. In the snippets below, about 1/3 of the `setTimeout()` callbacks will be called, although they have been scheduled in cleared timeouts.
See the second snippet for a more illustrative example of the issue that shows that some `setTimeout()` callbacks are executed even after their timeouts have been cleared.
### Expected behavior
Timeouts must never be executed after `clearTimeout()` with their id is called.
### Environment
- Tabris.js version: starting from 3.0.0 to at least 3.5.0-dev.20200514
- OS: iOS
### Code snippet
#### Simple snippet
```javascript
import 'tabris';
for (let i = 0; i < 3000; i++) {
const timeout = setTimeout(() => console.error('unexpected callback call'), 1000);
setTimeout(() => clearTimeout(timeout));
}
```
#### Verbose snippet
```javascript
import 'tabris';
let taskId = 0;
const status = {};
for (let i = 0; i < 3000; i++) {
taskId++;
status[taskId] = 'scheduled';
const timeout = setTimeout((id =>
() => {
let message = `Called task ${id}`;
if (status[id] === 'canceled') {
message += ' although it was canceled. clearTimeout does not work?'; // about 1/3 of the tasks
} else if (status[id] === 'scheduled') {
message += ' as expected, because it was scheduled and never canceled. clearTimeout called too late?'; // never
}
console.error(message);
}
)(taskId), 1000);
setTimeout((id =>
() => {
clearTimeout(timeout);
status[id] = 'canceled';
}
)(taskId), 0);
}
```
Contributor guide
Assessment
This issue has not been assessed yet.