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(){

    setTimeout(searchhh, 500);

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

Anyone can help please?

  • 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 Apr 2 '19 at 11:57
  • infallibletechie.com/2012/06/… i we used same thing for one of my task. – Umesh Beti Apr 2 '19 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 Apr 2 '19 at 14:16

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

var timerId;
function searchForAccount() {

     function searchhh(){
        timerId = null;

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.