Description
I have implemented a print report feature in Angular.
When the user clicks the Print button, the browser’s print dialog opens (using window.print()).
At the same time, I also have a ping mechanism to check if the connection between Angular (frontend) and .NET (backend) is active. If no ping response is received within 30 seconds, the UI shows an error message:
“Lost connection to server”
The problem is: when the print window is open, all JavaScript execution is paused (including the ping mechanism). If the user leaves the print window open for more than 30 seconds, Angular displays the “Lost connection to server” error even though the connection is fine.
Steps to Reproduce
- Implement a ping/heartbeat mechanism in Angular that checks server
connection every few seconds.
- Trigger window.print() from a button click.
- Keep the print window open for longer than the ping timeout
(30 seconds in my case).
- Observe that the ping stops during print
window → after timeout, Angular shows Lost connection to server.
Expected Behavior
The ping/heartbeat mechanism should continue working in the background (or resume seamlessly after print dialog is closed) so that the app does not incorrectly show Lost connection to server.
Actual Behavior
When the print dialog is open, JavaScript execution halts (including setInterval, setTimeout, and HTTP calls).
As a result, the ping never executes, and Angular incorrectly shows a server disconnection error after 30 seconds.
Environment
-
Angular version: 18
-
Browser(s): Chrome, Edge, etc.
-
OS: Windows 11
Question
Is there any Angular-recommended workaround or best practice to handle this scenario where background tasks (like ping/heartbeat) stop working when the print window is open?