Action Poller in Visualforce

Action Poller in visualforce is like a timer that sends an AJAX request to the server according to a time interval that you specify (time interval has to be specified or else it defaults to 60 seconds). 

In this article I will demonstrate how to use actionpoller in visualforce page.

  • In the action attribute a controller method gets called. The method gets called with a frequency defined by the interval attribute which has to be greater than or equal to 5 seconds.
  • We can provide comma separated id’s of one or more components that are refreshed when request completes. (which is basically called reRendering).

In the example below, action poller calls the method “callactionPoller” every 5 seconds. In callactionPoller, the variable “seconds” counter is incremented by 5 and the variable “noOFtimesCalled” describes how many times action poller has invoked the method. Rerender attribute refreshes the outputText, so seconds value and noOFtimesCalled value in the page will be refreshed.

Visualforce Code:

<apex:page controller="actionpollerDemoController" setup="false" showHeader="true" sidebar="false" tabStyle="Contact">
    <apex:form >
        <apex:pageBlock id="myPageId">
            <apex:pageBlockSection title="ActionPoller Example" collapsible="false" columns="1">
              <apex:actionPoller action="{!callactionPoller}" reRender="out,counter" interval="5"/>
              <apex:outputText value="{!seconds}" label="Total Time in seconds since action poller is called:" id="out"/>
                <apex:outputText value="{!noOFtimesCalled}" label="No of Times Called" id="counter"/>
            </apex:pageBlockSection>
         </apex:pageBlock>
      </apex:form>
</apex:page>

Apex Code:

public class actionpollerDemoController {
public  Integer seconds{get;set;}
public  Integer noOFtimesCalled{get;set;}
  public actionpollerDemoController(){
   seconds = 0;
   noOFtimesCalled = 0;   
  }
 
  public void callactionPoller(){
    seconds = seconds + 5;
    noOFtimesCalled+=1;  
  }
}

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *