I have a vf page displaying several Multi select custom buttons. And I am calling a apex method every time one of the button is clicked from VF page using actionfunction. But if I click 2 buttons in quick succession, the apex method being executed for the second time for the second button happens before the apex method for the first button finishes leading to unexpected result. if I wait for a little bit inbetween, the results come correct.
I tried using a Boolean 'methodExecuted' which will only execute the Javascript method after the Apex method gets executed. Didn't work.
Below similar Code
VisualForce
<apex:page standardcontroller="Opportunity" extensions="extensionclass">
<apex:slds/>
<script type="text/javascript">
function ButtonSelected(BtnValue) {
ButtonSelected(BtnValue);
if({methodExecuted}){
// Code to change the CSS style of button onclick
}
}
</script>
<apex:actionFunction action="{!ButtonAction}" name="ButtonSelected" reRender="dummy">
<apex:param name="param" value=""/>
</apex:actionFunction>
<apex:pageBlock>
<apex:repeat value="{!allButnValues}" var="Btn" id="dummy">
<button onclick="ButtonSelected('{!Btn.value');" id="{!btnId}">
{!Btn}
</button>
</apex:repeat>
</apex:pageBlock>
</apex:page>
Class
public class extensionclass{
public Boolean methodExecuted {get;set;}
public PageReference ButtonAction(){
methodExecuted = false;
selectedOption = Apexpages.currentPage().getParameters().get('param');
// Some logic to get data from the clicked button.But before this method finishes the first time, the method gets executed again
methodExecuted = true;
return null;
}