I am working on searching the contact by its first name. Is it Feasible to make a call to server from javascript every time when user presses a key ? Controller :

global class checkcontactduplicateandsave
public String contactName { get; set; }
public String contactEmail { get; set; }
public String contactPhone { get; set; }    
public static Contact[] contact { get; set; }
public checkcontactduplicateandsave()
{//default constructor}    
global static Contact[] getContact(String contactName ,String contactPhone,String contactEmail) 
        contact=[select ID,Name,MobilePhone,Email from Contact where FirstName=: contactName OR MobilePhone=: contactPhone OR Email =: contactEmail];
    return contact;


<script type="text/javascript">
    function getcontactJS() 
        //get the values of input text and place into the variable.

        var contactNameJS = document.getElementById('conName').value;
        var contactEmailJS = document.getElementById('conEmail').value;
        var contactPhoneJS = document.getElementById('conPhone').value;
        function(result, event)
            if (event.status) 
                for(var i=0;i<result.length;i++)
                        var acc = result[i];
                        document.getElementById('resultant').innerHTML += acc.Id;
                        document.getElementById('resultant').innerHTML += acc.Name + '<br/>';
            else if (event.type === 'exception') 
                document.getElementById("errors-js").innerHTML = event.message;
                document.getElementById("errors-js").innerHTML = 'No Records Found..';
        }, {escape:true});
    <td>Contact Name :</td>
    <td><input id="conName" type="text" onkeyup="getcontactJS();" /></td>
    <td>Contact Phone No :</td>
    <td><input id="conPhone" type="text" onkeyup="getcontactJS();" /></td>
    <td>Contact Email :</td>
    <td><input id="conEmail" type="text" onkeyup="getcontactJS();" /></td>
    <div id="errors-js"></div>
    <div id="resultant"></div>        

Is it feasible to have a server call from javascript each time ? Please Suggest any alternative.

2 Answers 2


I would add a timeout function to make the call every time there is a change and it has been at least 500ms between changes. Let the user type in what they want to type in, and make 1 callout to the server 500ms later. You can adjust the delay if necessary.

var timer;
var interval = 500;

function delayExecute()
        timer = setTimeout(

    return true;

<input id="conName" type="text" onkeyup="return delayExecute();" />

There is no issue of using Javascript Remoting each time user press a button. But consider few things:-

  • Make sure it is not in for loop. Also, response return are always asynchronous so, in case if user click/key-up multiple times, it will provide multiple results. Button should be disabled/delayed until response is not returned.

  • Make sure that response will be returned in javascript time-out limit.

  • Variable declaration public static Contact[] contact { get; set; } will be reset with each transaction completion.

  • "There is no issue of using Javascript Remoting each time user press a button" - Performance matters Oct 19, 2016 at 6:38

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.