I have a situation where I call a bit of javascript and it needs to do some housekeeping of data that is not associated with the current window after a small period of time has elapsed. However it seems that if I start a timer using window.setTimeout
, it'll be cancelled if the window gets closed. This is something I really don't want to happen. Is there a parent window of all windows that won't get closed till the browser quits, or anything else I can use.
Asked
Active
Viewed 37 times
-2

Tom Tanner
- 9,244
- 3
- 33
- 61
-
3If the window is closed what do you want it to clean up? – May 18 '18 at 21:21
-
1What do you mean by clean up? What is it that you want to clean? – snnsnn May 18 '18 at 21:23
-
it's data that is not associated with the window on which I want to do some housekeeping (in this specific case, I don't want to write the data to disk every time it's updated, but would rather write it back after a few seconds when updates have stopped) – Tom Tanner May 18 '18 at 21:26
-
What do you mean write it back to disk? In an AJAX call? If that's the case, you should still make the AJAX call and have your _server_ hold on to the data, and use a timer there to write it back to disk. – Dave May 18 '18 at 21:48
1 Answers
2
You could use the window.onUnload event or the window.onbeforeunload event to do whatever it is you need to do. Take a look at this previous question Run JavaScript code on window close or page refresh?

Brian Edwards
- 135
- 8
-
that's a thought but i really want to avoid any dependencies on a window as I'm trying to put this code into a module – Tom Tanner May 18 '18 at 21:35
-
@TomTanner In the browser, all Javascript is associated with a window, except maybe browser extensions. – Barmar May 18 '18 at 22:57