1

Is there any way to easily display to the user how long between actionPoller refreshes?

I tried using the onSubmit and onComplete event attributes in the actionPoller tag to call a timer function, but it seems inconsistent and desyncs fairly quickly.

What are some approaches I can take to display a (relatively) accurate time til refresh?

2

Its possible but not easy though, Used some JavaScript code from here.

Oncomplete of action poller, can write a function which resets the counter.

Class:

public class ActionPollarRemainingTime {

    // property to hold number of time refreshed
    public Integer timesRefreshed { get; set; }

    // constructor
    public ActionPollarRemainingTime() {
        timesRefreshed = 0;
    }

    // incrementing the counter
    public void incrementTimesRefreshed() {
        timesRefreshed++;
    }
}

Visualforce page:

<apex:page controller="ActionPollarRemainingTime">
    <apex:form>

        <!-- oncomplete is important here -->
        <apex:actionPoller reRender="theBlock" 
            interval="60" oncomplete="resetCounter();"
            action="{!incrementTimesRefreshed}"/>
        <apex:pageBlock id="theBlock">
            <apex:pageBlockSection>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Remaining Time"></apex:outputLabel>  
                    <!-- Following will be updated by JavaScript -->
                    <div>Refreshes in <span id="time">01:00</span> minutes!</div>
                </apex:pageBlockSectionItem>
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Count"></apex:outputLabel>  
                    <apex:outputText value="{!timesRefreshed}"></apex:outputText>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>

    <script>

        // reset counter
        function resetCounter() {
            var timeInMinutes = 1;
            var minutes = 60 * timeInMinutes,
                display = document.querySelector('#time');
            startTimer(minutes, display);
        }

        // start timer
        function startTimer(duration, display) {
            var timer = duration, minutes, seconds;
            setInterval(function () {
                minutes = parseInt(timer / 60, 10);
                seconds = parseInt(timer % 60, 10);

                minutes = minutes < 10 ? "0" + minutes : minutes;
                seconds = seconds < 10 ? "0" + seconds : seconds;

                display.textContent = minutes + ":" + seconds;

                if (--timer < 0) {
                    timer = duration;
                }
            }, 1000);
        }

        // start remaining time calculation on load
        window.onload = resetCounter();


    </script>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy