When working with XPages' server-side JavaScript (SSJS), I miss the timing/scheduling functions setTimeout
, setInterval
, clearTimeout
and clearInterval
.
Is there, or can someone provide, a polyfill for these essential functions in SSJS?
When working with XPages' server-side JavaScript (SSJS), I miss the timing/scheduling functions setTimeout
, setInterval
, clearTimeout
and clearInterval
.
Is there, or can someone provide, a polyfill for these essential functions in SSJS?
SSJS is just a string which is invoked when a method is called. There is no "timing" functionality available because the SSJS engine is just "executed" during the request. When the request is finished, the SSJS code must be completed.
When using these functions in the browser or a runtime like node.js, your code is able to run "in the background", because the runtime is still "running" if a request is completed.
Just think about the following: To stop an setTimeout, you must use clearInterval. This requires a variable holding the reference to the timer. How do you store this variable in SSJS?
EDIT:
You could change the DemoExecutor class to get a handle to the SSJS Interpreter.
class DemoCallable implements Callable {
private final NSFComponentModule module;
private transient JavaScriptInterpreter jsInterpreter;
private transient FBSGlobalObject globalObject;
public DemoCallable(NSFComponentModule module, JavaScriptInterpreter jsInterpreter, FBSGlobalObject globalObject ) {
this.module = module;
this.jsInterpreter = jsInterpreter;
this.globalObject = globalObject;
}
...
}
This gives you access to the Interpreter and the global JS Object.
You can get the objects from view root:
facesContext.getViewRoot().getGlobalObject()
facesContext.getViewRoot().getJSInterpreter()