function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShaikAShaikA 

​How to call a method many times by using Javascript

HI,

I have a senario to call a method many times to navigate PDF page many times, please help me how can achieve this.

Vf Page:

<apex:page controller="PDFController" >
<script>
function myJavascriptFunc()
    {        alert('Entered Javascript') ;

        for(integer i=0; i<4; i++){
        alert('Entered Javascript') ;
        CallApexMethod(i) ;
        }
    }
</script>
<apex:form >
 <apex:actionFunction name="CallApexMethod" action="{!PDFGenerator}" rerender="mpb" />
<apex:commandButton onclick="myJavascriptFunc()" value="PDF Generator"/>
</apex:form>
</apex:page>

Class:

public with sharing class PDFController {

    public PageReference PDFGenerator()
    {
     PageReference pdf = Page.selectedCandidatesPDFPage;
        pdf.setredirect(true);
        return pdf;
    }
}


Regards,
Anand
bob_buzzardbob_buzzard
You can't make actionfunction calls in a loop like that, as you will introduce race conditions - each call is likely to take out the previous one.  If you need to call the same function multiple times you have to orchestrate things so that each call is allowed to complete and return control to the page before you make the next call.

You could do this by adding an actionstatus to the actionfunction, and specifying an oncomplete handler to be executed when the request has been handled by the server. This oncomplete handler can call a JavaScript function which updates the counter, and executes the action function again if necessary.