Timeout in Vaadin

Hi,

First of all, sorry for my english isn’t good :slight_smile:

I am working on a Vaadin project over a year (excellent framework by the way). I’ve come from PHP language and there is one thing that I miss (i’m not sure if there is a similar here) and it’s the “window.setTimeout”.

In php (javascript) I can call a function when passing a time (in milliseconds). There is a way to do this in Vaadin?

In most posts, people attach the timeout with the “session-timeout”, but i’m not interested in it :slight_smile:

I hope you can help me.

Thank you!

Hi,

You can use
ScheduledExecutorService
. If you want the scheduled task to update the UI, you need two more things: Enable
Push
support, and use
UI.access()
:

[code]
@Push
public class ExampleUI extends UI {

...

@Override
protected void init(VaadinRequest request) {

    ...
  
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    executorService.schedule(this::showNotification, 5000, TimeUnit.MILLISECONDS);
}

private void showNotification() {
    UI.getCurrent().access(() -> {
        Notification.show("It works!");
    });
}

}
[/code]Hope that helps :slight_smile:

Yeah, it works! This is exactly what I was looking for.

Thank you very much :slight_smile: