Activity Forums Salesforce® Discussions Action poller in visualforce

  • Anjali

    Member
    September 4, 2018 at 11:04 am

    Hi Shradha,

    Action poller acts as a timer in visualforce page. It is used to send an AJAX request to the server depending on the time interval (time interval has to be specified or else it defaults to 60 seconds).

    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 5 seconds.

  • madhulika shah

    Member
    September 4, 2018 at 11:04 am

    Hi shradha,

    Action poller sends AJAX request with a specified time interval. Each request can result in a full or partial page update.

    An <apex:actionPoller> must be within the region it acts upon.

  • Parul

    Member
    September 4, 2018 at 11:06 am

    Hello Shradha,

    Action poller acts as a timer in visualforce page. It sends an AJAX request to the server according to a time interval (time interval has to be specified or else it defaults to 60 seconds). Each request can result in a full or partial page update.
    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.
    Time out can also be specified as an attribute in actionpoller. Once the time out point is reached it stops making AJAX callout to the server and controller method is no more called.

     

    Thanks.

  • Neha

    Member
    September 4, 2018 at 11:11 am

    Hi Shradha,

    The following information may help you.

    apex:actionPoller
    A timer that sends an AJAX request to the server according to a time interval that you specify. Each request can result in a full or partial page update.

    An <apex:actionPoller> must be within the region it acts upon. For example, to use an <apex:actionPoller> with an <apex:actionRegion>, the <apex:actionPoller> must be within the <apex:actionRegion>.

    Considerations When Using <apex:actionPoller>

    • Action methods used by <apex:actionPoller> should be lightweight. It's a best practice to avoid performing DML, external service calls, and other resource-intensive operations in action methods called by an <apex:actionPoller>. Consider carefully the effect of your action method being called repeatedly by an <apex:actionPoller> at the interval you specify, especially if it's used on a page that will be widely distributed, or left open for long periods.
    • <apex:actionPoller> refreshes the connection regularly, keeping login sessions alive. A page with <apex:actionPoller>on it won't time out due to inactivity.
    • If an <apex:actionPoller> is ever re-rendered as the result of another action, it resets itself.
      Avoid using this component with enhanced lists.

    Example:

    <!-- Page -->

    <apex:page controller="exampleCon">
    <apex:form>
    <apex:outputText value="Watch this counter: {!count}" id="counter"/>
    <apex:actionPoller action="{!incrementCounter}" reRender="counter" interval="15"/>
    </apex:form>
    </apex:page>

    /*** Controller: ***/

    public class exampleCon {
    Integer count = 0;

    public PageReference incrementCounter() {
    count++;
    return null;
    }

    public Integer getCount() {
    return count;
    }
    }

  • shariq

    Member
    September 15, 2018 at 12:49 am

    Hi,

    In the following example action poller calls the method "CounterMethod" every 5 seconds where the variable "seconds" counter is updated. Rerender attribute refreshes the page block hence showing the updated value of variable "seconds".

    Controller Class:
    Public with sharing class actionpollerDemoController {
    Public  Integer seconds{get;set;}
    Public actionpollerDemoController(){
    seconds = 0;
    }

    Public void CounterMethod(){
    seconds = seconds + 5;
    }
    }

    Visualforce Page:

    <apex:page controller="actionpollerDemoController">
    <apex:form >
    <apex:pageBlock id="pgplck">
    <apex:actionPoller action="{!CounterMethod}" reRender="pgplck" interval="5"/>
    {!seconds } seconds since the action poller was called !!
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    Hope this helps.

Log In to reply.

Popular Salesforce Blogs