Well, it looks my script is getting some sort of "ban" from the browser when it is trying to run some process in the loop. The script is running in the iframe, loaded from another domain and it's visible.
It is getting a ban only when running inside the iframe.
The code looks like this:
app_cycle()
{
let self = this;
// Making some fast things here (1-2 ms long)
// Calculate the delay based on the real time value from (new Date())
// Resulting "dt" is always between 5ms and 37ms. The ideal value is 20ms
// Always remove old timer to avoid consecutive timers
if (this.tickTimer) {
clearTimeout(this.tickTimer);
this.tickTimer = null;
}
this.tickTimer = setTimeout(() => {
self.app_cycle();
}, dt);
}
So as you can see I am not making anything special, just the method app_cycle that runs each 20ms (it's relatively stable because I am recalculating dt according to external deviations to make the average frequency to 50 Hz).
The problem is: my application runs very slowly in the beginning. Right after page load, the setTimeout delays for 1000ms do not depend on the calculated dt value.
This situation longs for 65 seconds and then my application becomes running normally.
If I stop the application by closing an iframe with it and run it again (without reloading the parent main page), no 65-second delays occurred anymore.
After the main page reload I am getting the same delays.
It looks like something a "ban" from the browser (I've tested in Chrome and Firefox, same result) and the question HOW to avoid that ban.
Thank you very much!
Okay, there was a ban on the iframe. Chrome blocks hidden iframes that come from other domains (cross-domains) always. In my case the iframe is perfectly visible, however, the parent node (div) was set as "width: 100%" and "height: auto" so the children iframe was recognized by Chrome as "non-visible iframe". I think it's a Chrome bug, but in my case setting "min-height: 10px" for the parent div perfectly fixed the problem.
Hopefully, this solution (I spent 3 days to find it) will help anybody and save some time.