我的应用程序中有GWT Timer,应该每15分钟触发一次。这一般都很好。但是,当Gwt Fileupload
对话框打开时,TIMER不会被触发。
下面给出了一个描述我的问题的示例应用程序。在这里,我为每分钟安排计时器。现在点击Button
的{{1}}的“选择文件...”Fileupload
,打开FileUpload
Dialog
框。保持打开状态超过一分钟。计时器未被触发。我在IE8 / 9/10中检查了这个示例代码。在所有这些浏览器中,TIMER都没有被触发。
非常感谢任何帮助
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
public class FileuploadEx implements EntryPoint{
@Override
public void onModuleLoad() {
// TODO Auto-generated method stub
FileUpload upload = new FileUpload();
upload.setName("Select File..");
VerticalPanel panel = new VerticalPanel();
panel.add(upload);
RootPanel.get().add(panel);
Timer t = new Timer() {
@Override
public void run() {
runAlert();
}
};
t.schedule(60000);
}
public void runAlert(){
Window.alert("Timer triggered");
Timer t = new Timer() {
@Override
public void run() {
// TODO Auto-generated method stub
runAlert();
}
};
t.schedule(60000);
}
}
答案 0 :(得分:2)
Javascript是单线程的,因此某些对话框如alert和confirm会停止主线程,并且在对话框关闭之前不会执行定时器。
然而,文件浏览器通常不会在我测试的浏览器(Linux中的chrome和FF)中最少停止线程,因此它可能是您浏览器或操作系统中的问题。
检查此example的gwtupload,您可以看到当您上传大文件(大约800Mb,因为示例限制为1Mb)并打开浏览器选择器时,进度条,实际上使用计时器和ajax,继续更新。
[EDITED] 在花了更多时间测试浏览器后,现代浏览器工作,除了IE总是停止javascript线程。
我想除非微软修改他们的产品,否则问题无法解决。 因此,您可以在代码中执行的最好的事情是在文件选择器关闭时使会话失效。
与您的问题相关的会话到期,我会按此顺序执行:
基于不活动或固定的periode使服务器端的会话到期,因此当客户端询问服务器时,它将收到错误并将用户带到登录屏幕或其他任何内容。这种方法更可靠,使用更广泛。
如果您想使用JS,请在var中设置会话的开始时间,然后运行定期计时器以检查会话是否已过期。打开文件对话框时,计时器不会运行,但只要用户关闭对话框,它就会收到会话已过期的消息
final double limit = 1000 * 60 * 15; // 15 minutes
final double started = Duration.currentTimeMillis();
Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() {
public boolean execute() {
double now = Duration.currentTimeMillis();
if (now - started > limit) {
// your code to remove session objects here
Window.alert("Session expired");
return false;
}
return true;
}
}, 1000);