The Rich UI job scheduler is a
timer that you can use to
invoke a customized function after a specified number of milliseconds.
You can schedule multiple jobs and can cancel them in response to
runtime conditions.
Try the following example
in your workspace:
import com.ibm.egl.rui.widgets.Button;
import egl.javascript.Job;
handler MyHandler type RUIhandler { initialUI = [stopButton],
onConstructionFunction = initialization }
stopButton Button{text="Stop!", onclick ::= pleaseStop};
doThis Job{runFunction = myRunFunction};
function initialization()
doThis.repeat(1000);
end
function myRunFunction()
sysLib.writeStdOut(currentTime());
end
function pleaseStop(e event in)
doThis.cancel();
end
end
When you use the job scheduler, you must type
the following
import statement:
import egl.javascript.Job;
You cannot add that statement with the Ctrl+Shift+O mechanism that
is available for Widget types.
Two definitions explain the relationships:
- The run function is the function that is invoked
when the job is scheduled. The run function in this example is myRunFunction.
- The current function is the function that runs while
the timer is running.
The job scheduler is a variable
that is based on an EGL external
type named Job. When you declare the variable, you can set the following
properties:
- name
- Used
by the EGL debugger to identify the job scheduler. If you
omit this property, the value of name is
the variable name.
- runFunction
- Identifies the run function, which has no parameters and no return
value.
You can use a job scheduler to
invoke the following functions:
- schedule
(intmilliseconds)
- Sets a timer immediately for
the specified number of milliseconds
and causes a subsequent invocation of the run function. The invocation
occurs, at earliest, when the timer elapses or when the current function
ends, whichever happens last. If you omit milliseconds,
the invocation occurs, at earliest, as soon as the current function
ends.
- repeat
(intmilliseconds)
- Sets a timer immediately for
the specified number of milliseconds
and then causes repeated invocation of the run function.
The timer
is reset each time the run function starts. For each invocation of
the run function, including the first, the invocation occurs, at earliest,
when the timer elapses or when the current function ends, whichever
happens last.
- cancel()
- Cancels later invocations of the job.
The
invocation of a run function never interrupts the execution
of another function. For example, between the time when a job is scheduled
and the time when the invocation of the run function is possible,
the user might click a button to cause scheduling of an event handler.
In that case, the invocation of the job function waits at least until
the event handler invokes its own subordinate functions, if any, and
ends.
You can create multiple variables of type JOB and schedule
multiple
jobs and even invoke the same run function. In all cases, only one
function can run at a given time, and it runs to completion.
If
you use the same variable to reschedule a job, the previous
use of that variable is canceled.