当浏览器关闭时小程序会发生什么?
假设我有一个小程序在浏览器的页面中运行。 当用户关闭浏览器时会发生什么?
小程序是否收到通知,以便它可以在其一侧执行某种关闭操作(关闭向服务器打开的连接、清理静态变量……)?
另外,我假设相同的行为适用于页面刷新或页面导航(而不是浏览器关闭)。 浏览器保持打开状态,但小程序消失了。 虽然当你关闭浏览器时你也关闭了 JVM,所以我现在不确定。
谢谢, 新山
Let's suppose I have an applet running within a page in a browser.
What happens when the browser is closed by the user?
Is the applet notified so that it can perform some kind of close action on its side (closing connections opened to a server, cleaning static variables, ...)?
Also, I assume the same behavior would apply for a page refresh or page navigation (instead of browser close). The browser remains opened but the applet is gone. Although when you close the browser you also close the JVM so I'm unsure at this point.
Thanks,
JB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,应该在浏览器卸载对象之前调用destroy()方法。
destroy() 是 Java applet 的四个“生命周期方法”中的最后一个(其他的是 init()、 start() 和 stop() )。 实际上,它们会在不同的时间被调用,具体取决于您的浏览器和虚拟机。 如果您想确切地知道每个方法何时被调用,请在小程序中实现每个方法,然后 System.out 一些反馈。
理想情况下,destroy() 应该由环境调用,并且只应该调用一次。 如果 destroy() 似乎没有被调用,您可以声明一个公共的 Finalize() 方法,该方法调用 destroy。 您还可以尝试在窗口对象卸载时从 javascript 调用 destroy(),但再次确保您没有不必要地调用 destroy()。
Yes, the destroy() method should be called before the browser unloads the object.
destroy() is the last of four "life-cycle methods" of the Java applet (the others are init(), start(), and stop() ). They're actually called at different times depending on your browser and virtual machine. If you'd like to know exactly when each is called, implement each method within your applet, and System.out some feedback.
Ideally, destroy() should be called by the environment, and should only be called once. If it seems like destroy() is not being called, you might declare a public finalize() method, which calls destroy. You could also try to call destroy() from javascript as the window object unloads, but again, be sure that you're not calling destroy() unnecessarily.
大多数时候 destroy 会被调用,但是在关闭窗口的情况下它没有足够的时间来完成所需的任务。
刷新时有足够的时间,用 Backword <- 和 Forward -> 导航。
Most Of the times destroy will be called , but it dont get enough time to do required tasks in case of closing the window.
It gets enough time when refreshing , navigating with Backword <- and Forward ->