最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

memory - Are JavaScript timers immediately cleaned up when you close a browser tab? - Stack Overflow

programmeradmin7浏览0评论

Say I have created a timer via:

setTimeout()

(or I'm using setInterval())

Whilst that timer is ticking down, and before it expires, I decide to close the browser tab which that code is executing in. Does the browser automatically clean up the timer at that point (taking into account that I have not called clearTimeout() at all)?

I would like to know whether there is a possibility of code lingering in memory for long enough that, when the timeout value has been reached, it could still execute code. Or whether closing the tab means the timer is wiped from memory, and thus the function to call in the setTimeout() will never be executed.

Say I have created a timer via:

setTimeout()

(or I'm using setInterval())

Whilst that timer is ticking down, and before it expires, I decide to close the browser tab which that code is executing in. Does the browser automatically clean up the timer at that point (taking into account that I have not called clearTimeout() at all)?

I would like to know whether there is a possibility of code lingering in memory for long enough that, when the timeout value has been reached, it could still execute code. Or whether closing the tab means the timer is wiped from memory, and thus the function to call in the setTimeout() will never be executed.

Share Improve this question asked Jan 17, 2014 at 9:17 Jason EvansJason Evans 29.2k15 gold badges94 silver badges156 bronze badges 4
  • I would suggest that it varies dependent on the browse but for example with Chrome a tab is essentially a process in itself; that, when closed, is removed from memory. As for other browsers I can't see the benefit of keeping it in memory but I couldn't say for sure. – RichieAHB Commented Jan 17, 2014 at 9:20
  • No timeout will execute after the browser window it is associated with has been closed: "cleaned up" (as in GC'ed?) doesn't seem like much of a practical concern and is itself an implementation detail. – user2864740 Commented Jan 17, 2014 at 9:20
  • @richieahb Yeah, I imagine this could be different for different browsers. Though what you say about Chrome makes sense. user2864740 Cheers for that. Do you happen to have a link to somewhere I can reference to look that up? I need to pass details onto some colleagues about this question. – Jason Evans Commented Jan 17, 2014 at 9:23
  • @JasonEvans Not directly, although I think that the [documented] answer lies directly in the scope of the the window context/lifetime. – user2864740 Commented Jan 17, 2014 at 9:25
Add a ment  | 

3 Answers 3

Reset to default 7

setInterval and setTimeout are the global context (via window object) methods. Then you close the tab or window all contexts will remove. Then you open new tab/window all contexts will created.

The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed. Same goes for setTimeout(). You can give settieout to open window after 3 seconds on close. click and close. You will see that window wont be popping up.

See This

Both SetInterval() and SetTimeout() are a browser built-in schedulers. Which allows to setup function calls for execution after given period of time.

SetTimeout() can be cancelled by clearTimeout() method or on browser window closed.

SetInterval() method has same features as setTimeout. Which can be stopped by clearInterval call.

Functions used in setTimeout/setInterval are also referenced internally and tracked until plete, then cleaned up.

发布评论

评论列表(0)

  1. 暂无评论