当浏览器关闭时小程序会发生什么?

发布于 2024-07-08 07:24:46 字数 225 浏览 8 评论 0原文

假设我有一个小程序在浏览器的页面中运行。 当用户关闭浏览器时会发生什么?

小程序是否收到通知,以便它可以在其一侧执行某种关闭操作(关闭向服务器打开的连接、清理静态变量……)?

另外,我假设相同的行为适用于页面刷新或页面导航(而不是浏览器关闭)。 浏览器保持打开状态,但小程序消失了。 虽然当你关闭浏览器时你也关闭了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦纸 2024-07-15 07:24:47

是的,应该在浏览器卸载对象之前调用destroy()方法

destroy() 是 Java applet 的四个“生命周期方法”中的最后一个(其他的是 init() start()stop() )。 实际上,它们会在不同的时间被调用,具体取决于您的浏览器虚拟机。 如果您想确切地知道每个方法何时被调用,请在小程序中实现每个方法,然后 System.out 一些反馈。

理想情况下,destroy() 应该由环境调用,并且只应该调用一次。 如果 destroy() 似乎没有被调用,您可以声明一个公共的 Finalize() 方法,该方法调用 destroy。 您还可以尝试在窗口对象卸载时从 javascript 调用 destroy(),但再次确保您没有不必要地调用 destroy()。

public void finalize () {
    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.

public void finalize () {
    destroy();
}
白鸥掠海 2024-07-15 07:24:47

大多数时候 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 ->

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文