0

I have a search input on VF page which every character that printed on the keyboard will show the closest result, using actionFunction.

for example :

If I printed "uni" all accounts that they names include the name "uni" will appear for selecting.

every letter I tap in my keyboard is calling the controller and make a SOQL query.

I want to implement a change which calling the server base on a setTimout method - which won't call the method if the previous setTimeout function did not finish, so the server call will be reduced.

so far I have this :

<apex:input value="{!acc}" onkeyup="searchForAccount();" />

<apex:actionFunction name="searchAccount"  action="{!searchAccounts}"/>

 function searchForAccount() {

     function searchhh(){
        searchAccount();
     }


    setTimeout(searchhh, 500);
}

I want the server will be called again just after the first 500ms passed.

Anyone can help please?

3
  • i think this is not best way to populate your text box when you press a character by character. i would suggest use autocomplete functionality here for filling up the text box with limited and accurate results, this cane be done by using component and rremote method invocation calls.
    – Umesh Beti
    Commented Apr 2, 2019 at 11:57
  • infallibletechie.com/2012/06/… i we used same thing for one of my task.
    – Umesh Beti
    Commented Apr 2, 2019 at 12:01
  • Thank you! It's nice but I don't sure I have the time to start with new adjustments and changes at this phase of the project.
    – Salvation
    Commented Apr 2, 2019 at 14:16

1 Answer 1

2

You'll need to remember the previous "timer id" and cancel it:

var timerId;
function searchForAccount() {

     function searchhh(){
        timerId = null;
        searchAccount();
     }

    if(timerId) {
      clearTimeout(timerId);
    }
    timerId = setTimeout(searchhh, 500);
}
1
  • YES!!! Exactly what I looked for! Thanks!
    – Salvation
    Commented Apr 2, 2019 at 14:13

You must log in to answer this question.

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