Vaadin JavaScript弹出窗口仅在每个会话中第一次起作用 [英] Vaadin JavaScript popup only works first time in each session

查看:101
本文介绍了Vaadin JavaScript弹出窗口仅在每个会话中第一次起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,无论用户如何退出页面,都需要清理进程.我正在使用JavaScript来检测用户何时通过关闭标签页或关闭浏览器退出页面,并显示一个弹出窗口以确保他们知道当前进程将被停止.这个问题仅针对弹出窗口,我只是想澄清为什么需要一个弹出窗口!

I have an application which requires a process to be cleaned up regardless of how the user exits the page. I'm using JavaScript to detect when the user exits the page via tab or browser closing, and showing a popup to ensure they know the current process will be stopped. This question regards the pop-up only, I just wanted to clarify why I needed one!

当用户按下按钮开始该过程时,将调用以下代码:

When the user presses a button to start the process, the following code is called:

JavaScript.getCurrent().execute(
    "var beforeCloseListener = function (e) { var e = e || window.event; var message = " + LEAVE_PAGE_MESSAGE + "; if(e)       e.returnValue = message; return message; } " +
    "window.addEventListener('beforeunload', beforeCloseListener);"
);

当用户尝试关闭浏览器时,此代码将创建一个弹出窗口.出色的!下面的屏幕快照显示,我可以在Chrome开发人员工具中找到该方法.

This code creates a pop-up when the user tries to close the browser. Excellent! The screenshot below shows that I can find the method in Chrome developer tools.

Chrome开发者工具javascript搜索第一次尝试

但是,如果用户决定重新启动浏览器并导航到同一页面,然后再次单击该按钮,则当他们尝试关闭浏览器时,不会在弹出窗口中看到它们.

However, if the user decides to relaunch the browser and navigate to the same page, and click the button again, when they try to close the browser, they are not presented to with the pop-up.

此代码肯定会再次调用,但是javascript似乎没有实际附加.在Chrome开发人员工具中,搜索时不再能看到该方法.

This code is definitely called again, but the javascript does not seem to actually be attached. In Chrome developer tools, I can no longer see the method when I search.

Chrome开发者工具javascript搜索第二次尝试

我对Vaadin和JavaScript的了解还只是个初学者,因此我可能在这两者的生命周期中都缺少一些东西,但是我的问题是,为什么第二次不将JavaScript编译到我的应用程序中,但是第一次可以完美地工作吗?我感觉这里缺少一些Vaadin功能.

My knowledge of both Vaadin and JavaScript is fairly beginner so I could be missing something in the lifecycle of either here, but my question is, why would the JavaScript not be compiled into my application the second time through, but work perfectly the first time? I feel like there is some Vaadin feature I am missing here.

如果我调用或不调用适当的removeEventListener方法,则此行为似乎是相同的.尽管与我的其他相关问题一样,我认为我的removeEventListener被正确触发了.

This behaviour seems to be same if I do or do not call the appropriate removeEventListener method. Though as in my other, related question, I don't think my removeEventListener is being fired correctly.

注意:在我测试过的浏览器中,这种行为是相同的; Firefox,Chrome,Safari和Opera.

Note: this behaviour is the same across the browsers I have tested on; Firefox, Chrome, Safari and Opera.

推荐答案

我现在已经找到问题的答案.本质上,我是从ScheduledExecutorService内调用Vaadin的JavaScript.exectute()方法. JavaScript不喜欢异步调用,因此我在UI.getCurrent().access()块中调用了必需的方法.我的印象是,这将导致我需要在UI线程上同步运行的javascript代码.显然并非如此.

I have now found the answer to my problem. Essentially, I was calling Vaadin's JavaScript.exectute() method from within a ScheduledExecutorService. JavaScript does not like being called asynchronously, so I had the required methods being called within a UI.getCurrent().access() block. I was under the impression that this would cause the javascript code I need to be run synchronously on the UI thread. Apparently this was not the case.

将我的javascript.execute()调用移出执行程序并进入主线程已解决了我的问题,因为在我的情况下,我的JavaScript可以同步运行是可以接受的.我将更多地研究UI.access()方法,以查看我预期的行为是否有效,如果不能运行,是否还有其他更好的方法可以使JavaScript在异步环境中运行.如果希望发现有用的信息,希望将这些信息添加到我的答案中.

Moving my javascript.execute() calls outside of the executor and into the main thread has solved my problem, as in my case it is acceptable for my JavaScript to be run synchronously. I will be looking more into the UI.access() method to see whether the behaviour I had expected is meant to work and if not, whether there are any better alternatives to get the JavaScript to run in an asynchronous environment. I hope to add this information to my answer if I find anything useful.

这篇关于Vaadin JavaScript弹出窗口仅在每个会话中第一次起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆