• ADF
    July 12, 2012

Calculating time since last query in adf using java script

Arunkumar Ramamoorthy
Consulting Technical Manager

In some scenarios, we would need to calculate the time elapsed since the
last query execution, without contacting the server (i.e without using
af:poll component). This blog is to give an example for this scenario by
using javascript.

OTN question : https://forums.oracle.com/forums/thread.jspa?forumID=83&threadID=2411997

that we have an application with a table in the page, and a
commandButton to query the records, we will proceed further to build our

The logical flow of this implementation would be like,
1. Execute the query
2. When the button clicked, reset the previous time
3. Set the time to 0.
4. Trigger the timer to increment time.

Now, implementation comes.

Lets add an output text to display the time lapsed

<af:outputLabel value="Minutes since last query :" id="ol1"/>
        <af:outputText value="" id="ot9" clientComponent="true"/>

Since we would be accessing the output text from javascript, we would need to make it available to client by setting clientComponent to true.

We now need to add a javascript code to our page, to count the minutes and display in the output text.

        <af:resource type="javascript">

        function updateTimer(){
            var opTxt = AdfPage.PAGE.findComponentByAbsoluteId('ot9');
            var curMins = opTxt.getProperty('value');
            if(curMins == '' || curMins == null)
                opTxt.setProperty('value',parseInt(opTxt.getProperty('value')) + 1);


In the above code, we find the outputText component (with id ot9), get its value. If the value is blank or null, then set it to 0. If it is a non-zero number, then increment it.

Now, we need to call this method every minute to update the output text. For this, we will add another JS function.

        function startTimer(){
            var opTxt = AdfPage.PAGE.findComponentByAbsoluteId('ot9');
            var int=window.setInterval("updateTimer()",60000);

This function finds the outputText (with id ot9), resets its value (i.e set it to blank) and then schedules the function we've created first (updateTimer()) to be called every minute (i.e 60000 milliseconds).

Assuming we have a button to execute the query, we'll add a client listener to the button to execute this function when pressed.

                <af:commandButton actionListener="#{bindings.Execute.execute}" text="Execute"                                       
                                        disabled="#{!bindings.Execute.enabled}"  id="cb1" partialSubmit="true">
                          <af:clientListener method="startTimer" type="action" />

Ouput would be like below.

Join the discussion

Comments ( 2 )
  • Mohammad Jabr Sunday, July 15, 2012

    Arunkumar, Thank you again to blog about this.

    However,This method has one drawback, when the use click the browser's refresh button the timer is reset. Do you have any suggestion for this?

  • Arun Tuesday, July 17, 2012


    You may want to set the value to a cookie. And on load of the document, if the cookie value is not null, then start the timer again. Of course, you need to take care of expiring the cookie (otherwise, you will end up getting a older value if you re-run the application).


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.