How to perform timed action ?
if you need to fire action method calls (ajax calls to server) periodically, just like a timer, you need to use actionPoller tag. This will call the action methods at regular intervels & rerender the section of the screen(optionally).Where is the code ?
The actionpoller tag can be illustration with following example. This updates the userRetries count everytime it sends the request to server. Finally, when it reached 10 times, it quits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="testcls4_06122016" > | |
<apex:form> | |
<apex:actionPoller interval="5" action="{!timerFired}" | |
enabled="{!userRetries < 10}" reRender="UserMessage" /> | |
<apex:outputPanel id="UserMessage"> | |
User Retry Count - {!userRetries} | |
</apex:outputPanel> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class testcls4_06122016 { | |
public Integer userRetries{get;set;} | |
public testcls4_06122016() { | |
userRetries = 0; | |
} | |
/* Action Methods */ | |
public void timerFired() { | |
userRetries++; | |
} | |
} |
Precautions ?
you need to keep following things in mind, before using this tag- Since, this calls repeatedly by itself & can run very silently in browser, it should be used only if it's required.
- One advantage of using this method is that it keeps the session alive & user login cannot timeout.
- The best practice is to avoid heavy DML, Call out, Trigger logic through it.
No comments:
Post a Comment
Thanks for reading my blog !