Package com.oorian.html.js
Class TimerApi
java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.TimerApi
Provides timer functionality from server-side Java code.
This class provides convenient access to both client-side and server-side timers.
Timer Types:
- Client-side timers - Execute JavaScript in the browser using
setClientTimeout(String, long)andsetClientInterval(String, long). - Server-side timers - Execute on the server using Java's Timer via
setTimeout(Runnable, long)andsetInterval(Runnable, long, long).
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidclearTimer(SimpleTimer timer) Cancels a server-side timer.static voidsetClientInterval(HtmlPage page, String script, long intervalMs) Executes JavaScript code repeatedly at a fixed interval.static voidsetClientInterval(String script, long intervalMs) Executes JavaScript code repeatedly at a fixed interval using the current page context.static voidsetClientTimeout(HtmlPage page, String script, long delayMs) Executes JavaScript code after a delay.static voidsetClientTimeout(String script, long delayMs) Executes JavaScript code after a delay using the current page context.static SimpleTimersetInterval(Runnable task, long delayMs, long intervalMs) Creates a server-side periodic timer that executes a task at regular intervals.static SimpleTimersetInterval(String id, Runnable task, long delayMs, long intervalMs) Creates a server-side periodic timer with an ID that executes a task at regular intervals.static SimpleTimersetTimeout(Runnable task, long delayMs) Creates a server-side one-shot timer that executes a task after a delay.static SimpleTimersetTimeout(String id, Runnable task, long delayMs) Creates a server-side one-shot timer with an ID that executes a task after a delay.
-
Constructor Details
-
TimerApi
public TimerApi()
-
-
Method Details
-
setClientTimeout
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
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
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
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
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
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
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
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
Cancels a server-side timer.This is equivalent to JavaScript's
clearTimeout()orclearInterval().- Parameters:
timer- The timer to cancel.
-