0

What I'm trying to do is to call function from apex controller on mu VF page with timeout. For this purpose I've decided to add next code <apex:actionFunction name="searchAuthProducts" action="{!searchAuthorizedProducts}" reRender="none" onComplete="rerenderResults()"/>

also I have js script -

 <script>
var timer;
    function searchWithTimeout(){
        this.timer = setTimeout(searchAuthProducts(), 10000);

    }

    function resetTimer(){           
        clearTimeout(this.timer);        
    }

And VF field that invoke js method -

<apex:inputText value="{!productName}" styleClass="slds-input" onKeyUp="searchWithTimeout();" onKeyDown="resetTimer();"/>

But it doesn't work, function fired right after OnKeyUp. Do you have any ideas why?

2 Answers 2

2

Yes, by adding the parentheses, you're actually calling the function. Instead, you want to pass in the function itself as a reference. To do this, pass in just the function's name without the parentheses.

// Will be called after 10 seconds if not cancelled
this.timer = setTimeout(searchAuthProducts, 10000);
0

I've solved this.

Here is my solution vf input:

 <apex:inputText value="{!productName}" styleClass="slds-input" onKeyDown="searchWithTimeout();"/>

js code:

<script>
var timer;

function searchWithTimeout(){
        clearTimeout(timer);
        timer = setTimeout(()=>{searchAuthProducts()}, 1200);
    }
</script>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .