Skip to main content

Interval

The Interval class represents a timer that triggers an event with a fixed time delay between each triggering.

The Interval class provides a straightforward way to trigger events after a specified delay. It's possible to start, stop, and restart an Interval as needed. Additionally, Intervals can support multiple listeners for the elapsed event. Optimized for the webforJ framework, it offers better performance compared to the standard Java timer or the Swing timer.

Usages

The Interval class triggers event(s) at a fixed time delay. Leveraging Intervals creatively, you can enhance user interaction and engagement on your website while keeping the experience dynamic and interesting.:

  1. Check for Inactivity: Display a Dialog component if there hasn't been any interaction on a form within a given time.

  2. Featured Content: Rotate through featured articles, products, or promotions on your homepage on each Interval. This keeps the content dynamic and engaging.

  3. Live Data: Refresh data on your app, such as stock prices, news feeds, or weather updates, on each Interval to keep the data current.

Managing Interval States: Starting, Stopping, and Restart

An Interval requires manual activation; use the start() method to initiate it. To stop an Interval, use the stop() method. The restart() method can be used to restart the Interval.

Adjusting the Interval Delay

To modify the delay of an Interval, use the setDelay(float delay) method. The new delay value is applied after the Interval is either stopped or restarted.

//Changing the Delay
Interval.setDelay(2f);
Interval.restart();
tip

The delay can be fractional seconds to millisecond in resolution, but a very small timeout value causes a flood of events faster than the program can respond to them.

Adding Listeners

You can attach additional listeners to an Interval using the addElapsedListener(EventListener\<Interval.ElapsedEvent\> listener) method. Once a listener is added, it automatically triggers on the next interval if the Interval is already running.

// Adding Listeners
float dealy = 2f;

EventListener<Interval.ElapsedEvent> firstListener = (e -> {
// Executable code
});

Interval interval = new Interval(delay, firstListener);

EventListener<Interval.ElapsedEvent> secondListener = (e -> {
// Executable code
});

interval.addElapsedListener(secondListener);