Class TimeoutTrigger

java.lang.Object
com.vaadin.flow.component.trigger.internal.Trigger
com.vaadin.flow.component.trigger.internal.TimeoutTrigger
All Implemented Interfaces:
Serializable

public class TimeoutTrigger extends Trigger
Fires once, on the client, after the given delay has elapsed. The delay is measured by the browser via a single setTimeout armed when the host is initialized on the client; when it elapses the bound actions run. Pair it with a value-less CallbackAction (or any other action) to run server-side work a short while later without enabling push.

Unlike event- or observer-based triggers, this one fires a single time. The timer is cleared if the trigger is removed before it elapses; because it lives in the browser, it also never fires if the host's client DOM goes away (page reload, navigation, ?) first.

For internal use only. May be renamed or removed in a future release.

Since:
25.2
See Also:
  • Constructor Details

    • TimeoutTrigger

      public TimeoutTrigger(Component host, Duration delay)
      Creates a trigger that fires once, the given delay after the host is initialized on the client.
      Parameters:
      host - the component whose client-side initialization arms the timer, not null
      delay - how long to wait before firing, not null and not negative
  • Method Details

    • install

      protected Registration install(JsFunction action)
      Description copied from class: Trigger
      Installs the given rendered action as a client-side listener and returns the Registration that detaches it. Called once per action passed to Trigger.triggers(Action...).

      Implementations typically pass getHost().addJsInitializer an initializer that hands the action JsFunction to whatever client API the trigger wraps. The initializer is declared as a snippet, so that nothing is compiled from a string in the browser:

       @JsExpression("this.addEventListener($1, $0);"
               + "return () => this.removeEventListener($1, $0);")
       JsFunction listenToEvent(JsFunction action, String eventName);
      
       // In install, with LISTEN_JS = JsSnippets.of(ListenJs.class)
       return getHost().addJsInitializer(
               LISTEN_JS.listenToEvent(action, eventName));
       
      The initializer runs with this bound to the host element, and returns the function that uninstalls it. The arguments of the snippet are made available as $0, $1, ? in the order passed.
      Specified by:
      install in class Trigger
      Parameters:
      action - the rendered action JsFunction; takes one runtime argument named event (the trigger's event payload), not null
      Returns:
      the registration whose Registration.remove() detaches the listener, not null