Microsoft MakeCode

Time and switching tabs

How does time work in the simulator wrt switching tabs?

I’ve noticed that if I run the code mentioned in Physics engine tests? and switch to another tab in Chrome for a few seconds while the code is running it appears that the timer set by start countdown continues running but the code does not? Is that the intended behaviour?

If the simulator is intentionally pausing when the user goes off to use another tab in the same window then then shouldn’t it also pause the simulator’s view of time for that duration?

This depends on the browser, but most browsers will assign less priority to background tabs (those that aren’t active). So I assume the simulator will be affected by this for sure. I do not think the simulator acts on being inactive (like pausing), but just runs with a lower priority … let someone correct me if I am wrong.

I’m running the browser on a quiet multiprocessor desktop so there’s not an obvious lack of resources in play here.

On the subject of time, I’m also interested in another aspect. What does time since start (ms) return? Is this returning the simulation time in quanta related to the game loop or is it a free-running clock, i.e. successive calls within something like a large block of code in an on game update block are likely to return different values?

“Time since start” is the wall-clock time since the program began. It pauses if you are debugging (i.e. stepping through code, hitting breakpoints, etc.), but otherwise it is equal to the time that has elapsed in the browser/hardware clock since the program began.

The simulator doesn’t do anything special when you change tabs; it keeps running but is throttled by the browser. I believe all modern browsers always throttle background tabs.

Thanks, perhaps I was being too impatient on letting it finish.

Chrome behaviour changes a lot with audio use. If I do a bit of continuous beeping then it will run when it’s not visible (user on another tab) at or close to normal speed, at least for that demo code I mentioned.