Window loses focus, timer stops?

You didn’t specify which platform(s) you see this on. But if you are talking about macOS, you may be the victim of a “feature” called “app nap” where Apple tries to minimize resource usage (particularly battery, cpu, or active memory) and automagically put a GUI app to sleep while the window is not visible – i.e. minimized or fully covered by other applications.

And a long time ago, Apple exposed an easy way in the macOS SDK to disable that but later macOS SDK versions do not offer that anymore. You may find parts of this thread educational for ways to still disable it from your code.