Class TimerApi


public class TimerApi extends JavaScriptApi
Provides timer functionality from server-side Java code.

This class provides convenient access to both client-side and server-side timers.

Timer Types:

Usage:


 // Client-side one-shot timer (executes JS after 3 seconds)
 TimerApi.setClientTimeout("console.log('Hello!')", 3000);

 // Client-side periodic timer (executes JS every 5 seconds)
 TimerApi.setClientInterval("refreshDisplay()", 5000);

 // Server-side one-shot timer (fires after 3 seconds)
 SimpleTimer timeout = TimerApi.setTimeout(() -> {
     System.out.println("Timeout!");
 }, 3000);

 // Server-side periodic timer (fires every 1 second after initial 500ms delay)
 SimpleTimer interval = TimerApi.setInterval(() -> {
     refreshData();
 }, 500, 1000);

 // Cancel a server-side timer
 TimerApi.clearTimer(interval);
 
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • TimerApi

      public TimerApi()
  • Method Details

    • setClientTimeout

      public static void setClientTimeout(String script, long delayMs)
      Executes JavaScript code after a delay using the current page context.

      This is equivalent to JavaScript's setTimeout().

      Parameters:
      script - The JavaScript code to execute.
      delayMs - The delay in milliseconds before executing.
    • setClientTimeout

      public static void setClientTimeout(HtmlPage page, String script, long delayMs)
      Executes JavaScript code after a delay.

      This is equivalent to JavaScript's setTimeout().

      Parameters:
      page - The page to execute the script on.
      script - The JavaScript code to execute.
      delayMs - The delay in milliseconds before executing.
    • setClientInterval

      public static void setClientInterval(String script, long intervalMs)
      Executes JavaScript code repeatedly at a fixed interval using the current page context.

      This is equivalent to JavaScript's setInterval().

      Parameters:
      script - The JavaScript code to execute.
      intervalMs - The interval in milliseconds between executions.
    • setClientInterval

      public static void setClientInterval(HtmlPage page, String script, long intervalMs)
      Executes JavaScript code repeatedly at a fixed interval.

      This is equivalent to JavaScript's setInterval().

      Parameters:
      page - The page to execute the script on.
      script - The JavaScript code to execute.
      intervalMs - The interval in milliseconds between executions.
    • setTimeout

      public static SimpleTimer setTimeout(Runnable task, long delayMs)
      Creates a server-side one-shot timer that executes a task after a delay.

      This is equivalent to JavaScript's setTimeout().

      Parameters:
      task - The task to execute when the timer expires.
      delayMs - The delay in milliseconds before executing the task.
      Returns:
      The created SimpleTimer instance.
    • setTimeout

      public static SimpleTimer setTimeout(String id, Runnable task, long delayMs)
      Creates a server-side one-shot timer with an ID that executes a task after a delay.

      This is equivalent to JavaScript's setTimeout().

      Parameters:
      id - The unique identifier for this timer.
      task - The task to execute when the timer expires.
      delayMs - The delay in milliseconds before executing the task.
      Returns:
      The created SimpleTimer instance.
    • setInterval

      public static SimpleTimer setInterval(Runnable task, long delayMs, long intervalMs)
      Creates a server-side periodic timer that executes a task at regular intervals.

      This is equivalent to JavaScript's setInterval().

      Parameters:
      task - The task to execute at each interval.
      delayMs - The initial delay in milliseconds before the first execution.
      intervalMs - The interval in milliseconds between subsequent executions.
      Returns:
      The created SimpleTimer instance.
    • setInterval

      public static SimpleTimer setInterval(String id, Runnable task, long delayMs, long intervalMs)
      Creates a server-side periodic timer with an ID that executes a task at regular intervals.

      This is equivalent to JavaScript's setInterval().

      Parameters:
      id - The unique identifier for this timer.
      task - The task to execute at each interval.
      delayMs - The initial delay in milliseconds before the first execution.
      intervalMs - The interval in milliseconds between subsequent executions.
      Returns:
      The created SimpleTimer instance.
    • clearTimer

      public static void clearTimer(SimpleTimer timer)
      Cancels a server-side timer.

      This is equivalent to JavaScript's clearTimeout() or clearInterval().

      Parameters:
      timer - The timer to cancel.