Timers and Displaying a Clock

An external clock can be constructed using timers. Timers correspond to internal clocks, which have a specific time period. When the specified duration expires, the timer can either perform an action once and stop or repeat the action regularly every time the timer expires . Timer duration is always in milliseconds . Timers are created using the CREATE_TIMER built in and require a WHEN-TIMER-EXPIRED trigger to be written at the form level. This trigger fires every time the timer expires.

Using REPEAT Timers

Let's create a display item, CURRENT_TIME, in the horizontal toolbar canvas CANVAS_TOOLBAR created earlier. This item shows the time in HH24:MI:SS format and updates itself every second (the timer duration).

In the WHEN-NEW-FORM-INSTANCE trigger, create a timer named CLOCK_TIMER, which iterates after every one second and populates the CURRENT_TIME item with the system date in HH24:MI:SS format. The code is as follows :

DECLARE timer_id TIMER; one_second NUMBER := 1000; BEGIN timer_id := FIND_TIMER('CLOCK_TIMER'); IF NOT ID_NULL(timer_id) THEN DELETE_TIMER(timer_id); ELSE timer_id := CREATE_TIMER('CLOCK_TIMER',one_second, REPEAT); END IF; SELECT TO_CHAR(SYSDATE,'HH24:MI:SS') INTO :toolbar.current_time FROM DUAL; EXCEPTION WHEN OTHERS THEN MESSAGE(TO_CHAR(SQLCODE)''SQLERRM); END;

Create a WHEN-TIMER-EXPIRED trigger as follows:

DECLARE timer_name VARCHAR2(30); BEGIN timer_name := GET_APPLICATION_PROPERTY(TIMER_NAME); IF timer_name = 'CLOCK_TIMER' THEN SELECT TO_CHAR(SYSDATE,'HH24:MI:SS') INTO :toolbar.current_time FROM DUAL; END IF; EXCEPTION WHEN OTHERS THEN MESSAGE(TO_CHAR(SQLCODE)''SQLERRM); END;

Категории