Search Here

apex: actionPoller Component in Visualforce Page

The <apex:actionPoller> component is a special type of component that works based on time and interval. It is a timer used for sending an AJAX request to the server according to the specified time interval.


The <apex:actionPoller> component should be used within the region it acts upon. If we want to use the <apex:actionPoller> with the <apex: actionRegion>, the <apex:actionPoller> should be used within the <apex:actionRegion> component.


In order to use the <apex:actionPoller> component, we have to go through the following consideration:


The action method should be light-weighted, which we define in the <apex:actionPoller>. In the action method, we need to avoid using DML, external service calls, and other resource-intensive operations.

The connection is regularly refreshed by the <apex:actionPoller> component, so we must keep the login session alive. Due to inactivity, the apex page with the <apex:actionPoller> would not timeout.

It resets itself when the <apex:actionPoller> component is ever re-rendered as the result of another action.

For defining the time interval to server requests from the apex controller, we can't use the Visualforce expression.

We need to avoid using actionPoller with enhanced lists.

The <apex:actionPoller> component has the following attributes:


1. action

The action attribute is of type ApexPages.Action that defines the action method. The action method is invoked by the periodic AJAX update request from the component. The page simply refreshes when there is no action method defined in this component.


Syntax


<apex:actionPoller action= "{!getValue}"></ apex:actionPoller>  

2. enabled

The enabled attribute is of type Boolean that is used to specify the active and inactiveness of the <apex:actionPoller> component. By default, its value is set to true.


Syntax


<apex:actionPoller enabled = "false"></ apex:actionPoller>  

3. id

The id attribute is of type string that is a unique identifier allowing this component to be referenced by other components on the page.


Syntax


<apex:actionPoller id= "cmpId"></ apex:actionPoller>  

4. interval

The interval attribute is of type Integer that is used to specify the time interval between AJAX update requests in seconds. The value of this attribute must be 5 seconds or greater. By default, its value is set to 60 seconds.


Syntax


<apex:actionPoller interval= "10"></ apex:actionPoller>  

5. oncomplete

The oncomplete attribute is used to specify the JavaScript method invoked when the result of an AJAX update request completes on the client.


Syntax


<apex:actionPoller oncomplete= "resetCounter();"></ apex:actionPoller>  

6. onsubmit

The onsubmit attribute is used to specify the JavaScript method invoked before sending an AJAX update request to the server.


Syntax


<apex:actionPoller onsubmit= "resetCounter();"></ apex:actionPoller>  

7. rendered

The rendered attribute is of type Boolean that is used to specify whether the current component is rendered on the page or not. By default, its value is set to true.

AD


Syntax


<apex:actionPoller rendered= "false"></ apex:actionPoller>  

8. reRender

When we want to redraw one or more components on the page after receiving the result of an AJAX update request by the client, we use this component to specify the Ids of those components.


Syntax


<apex:actionPoller reRender= "cmp1, cmp2"></ apex:actionPoller>  

9. status

The status attribute is of type string that is used for specifying the id of the component responsible for displaying the status of an AJAX update request.


Syntax

AD


<apex:actionPoller status= "statusId"></ apex:actionPoller>  

10. timeout

The timeout attribute is used for defining the amount of time before an AJAX update request should timeout.


Syntax


<apex:actionPoller timeout= "5"></ apex:actionPoller>  

Let's take an example to understand how we can use the <apex:actionPoller> component in VF:


ApexActionPollerExample.vfp


<!-- Action Poller Page -->  

<apex:page controller="ApexActionPollerController">  

    <!-- apex form start-->  

    <apex:form>  

          

        <!-- oncomplete is important because we need to reset the counter after completing the AJAX request -->  

        <apex:actionPoller reRender="pbId" interval="60" oncomplete="resetCounter();" action="{!refreshTime}"/>  

          

        <!-- use pageBlock, pageBlockSection, pageBlockSectionItem for displaying time details -->  

        <apex:pageBlock id="pbId">  

            <apex:pageBlockSection>  

                <apex:pageBlockSectionItem>  

                    <apex:outputLabel value="Remaining Time"></apex:outputLabel>    

                    <div>Refreshes in <span id="timeSpan">01:00</span> minute!</div>  

                </apex:pageBlockSectionItem>  

                <apex:pageBlockSectionItem>  

                    <apex:outputLabel value="Count"></apex:outputLabel>    

                    <apex:outputText value="{!gerRefreshedCount}"></apex:outputText>  

                </apex:pageBlockSectionItem>  

            </apex:pageBlockSection>  

        </apex:pageBlock>  

    </apex:form>  

      

    <script>  

      

        // create resetCounter() methof to reset counter's value  

        function resetCounter() {  

            var min = 1;  

            var minutes = 60 * min  

            var display = document.querySelector('#timeSpan');  

              

            // call helper method to reset the counter  

            startTimer(minutes, display);  

        }  

          

        // start timer  

        function startTimer(period, display) {  

            var data = period, min, sec;  

              

            // use setInterval function  

            setInterval(function () {  

                // calculate minutes and seconds  

                min = parseInt(data / 60, 10);  

                sec = parseInt(data % 60, 10);  

                  

                // use the ternary operator to get minutes and seconds  

                min = min < 10 ? "0" + min : min;  

                sec = sec < 10 ? "0" + sec : sec;  

                  

                // set time details to the span  

                display.textContent = min + ":" + sec;  

                  

                if (--data < 0) {  

                    data = period;  

                }  

            }, 1000);  

        }  

          

        // start remaining time calculation on load  

        window.onload = resetCounter();  

    </script>  

</apex:page>  

ApexActionPollerController.apxc


public class ApexActionPollerController {  

      

    // gerRefreshedCount property for holding the number of times refreshed  

    public Integer gerRefreshedCount { get; set; }  

      

    // default constructor to set gerRefreshedCount to 0  

    public ApexActionPollerController() {  

        gerRefreshedCount = 0;  

    }  

      

    // create refreshTime() method for incrementing the counter  

    public void refreshTime() {  

        gerRefreshedCount++;  

    }  

}   

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.