This is a simulation: a Map stands in for the browser's list of active intervals, and tick() fires every active interval once. A price panel is opened and closed three times, first without cleanup, then with it (and opened once more). What does it log?
const intervals = new Map();
let nextId = 1;
const startInterval = (fn) => {
intervals.set(nextId, fn);
return nextId++;
};
const stopInterval = (id) => intervals.delete(id);
const tick = () => intervals.forEach((fn) => fn());
let requests = 0;
function mountPanel(withCleanup) {
const id = startInterval(() => {
requests += 1;
});
return withCleanup ? () => stopInterval(id) : () => {};
}
for (let visit = 1; visit <= 3; visit++) {
const unmount = mountPanel(false);
unmount();
}
tick();
console.log(intervals.size);
console.log(requests);
intervals.clear();
requests = 0;
for (let visit = 1; visit <= 3; visit++) {
const unmount = mountPanel(true);
unmount();
}
mountPanel(true);
tick();
console.log(intervals.size);
console.log(requests);