Reputation: 1
I have GWT Timer in my application which should trigger for every 15minutes. This is working fine in general. But, when Gwt Fileupload
dialog box is open, TIMER is not getting triggered.
Below given is a sample application which depicts my issue. Here I scheduled timer for every Minute. Now click the 'Select File.." Button
of Fileupload
which opens FileUpload
Dialog
box. Remain it open for more than a minute. The timer is not getting triggered. I checked this sample code in IE8/9/10. In all these browsers TIMER is not getting triggered.
Any help is highly appreciated
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);
}
}
Upvotes: 0
Views: 281
Reputation: 9741
Javascript is single threaded, so certain dialogs like alert and confirm stops the main thread and timers are not going to be executed until the dialog closes.
However file-browser normally does not stop the thread ant least in the browsers I've tested (chrome and FF in linux), so it could be an issue in your browser or your os.
Check this example of gwtupload, and you can see that when you are uploading a big file (about 800Mb because the example is limited to 1Mb) and you open the browser selector the progress bar, which actually uses timers and ajax, continues updating.
[EDITED] After spending a bit more time testing browsers, modern browsers work, except IE which always stops the javascript thread.
I guess there is no solution to the problem unless Microsoft modifies their product. So the best thing you can do in your code is to expire the session when the file selector closes.
Related with your problem to expire the session, I would do in this order:
Expire the session in server side based on inactivity or a fixed periode, so as when the client asks the server it will get an error and would bring the user to the login screen or whatever. This is the method more reliable and more widely used.
If you want to go with JS, set the start time of the session in a var, and then run a periodic timer to check whether the session has expired. When the file dialog is opened, the timer does not run, but as soon as the user closes the dialog it will get the message of session expired
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);
Upvotes: 2