5

I need to check if there is delay of 500 ms after a keystroke then i need to call a API. Can anyone help how should i proceed..how can i get this delay. Is there apex attribute using which i can do this.

Also if this can be done via javascript can someone give an example..

3 Answers 3

8

My understanding from your question is you want to call an API if there's a delay of 500ms or more between 2 keystrokes. If my understanding is correct then you could use the below script

var previousTimeStamp =0;
$("#someInputText").bind('keypress',function(event){
var diff = event.timeStamp-previousTimeStamp;
    console.log('the diff is '+ diff); 
if(diff>=500 && diff!=event.timeStamp){
    alert('waited for than 500 milliseconds');
            //call your API's now.. 
}
previousTimeStamp = event.timeStamp;
});

$("#someInputText").bind('blur',function(){
   previousTimeStamp = 0;
});

You can check it out here: http://jsfiddle.net/6GZH7/3/

0
3

There is no method to prevent statement execution for a specific time by apex code. Salesforce is a multi tenant architecture you can't force it to wait.

In javascript/jquery there is a timeout function:

$(document).keydown(function(e){

// On enter press
if(e.keycode == 13) {
    setTimeout(function() {
       console.log(' called after delay of 500ms');
    }, 500);
 //OR
 callAPI(); // existing actionFunction or javascript function
}
4
  • 3
    Just as a side note / clarification, the reason Apex doesn't have a sleep function isn't because it's multi-tenant; many (most?) multi-tenant platforms such as Amazon, GAE, Heroku etc expose a sleep function. Sleep functions are ridiculously cheap and efficient and multi-tenant is no excuse not to have one. The reason you can't sleep in Apex is simply because Salesforce has chosen not to offer a sleep function. There's an Idea page (over 4 years old) for this feature here: success.salesforce.com/ideaView?id=08730000000Bq2H
    – jkraybill
    Commented Sep 5, 2013 at 7:38
  • @jkraybill Thanks. By using system delay salesforce has to refactor governor limits.
    – Ashwani
    Commented Sep 5, 2013 at 7:42
  • 1
    Why would you even mention Apex Code here, though? Apex Code doesn't have a notion of "keypress" at all; that's Visualforce, and thus this question lands squarely in the middle of JavaScript territory. Think of an auto-suggest feature that calls a query when the user pauses.
    – sfdcfox
    Commented Sep 5, 2013 at 13:21
  • Therefore I provided javascript approach. My answer is not associating "keypress" with "ApexCode". Anyways It is possible to pass information from apex code to visulaforce page which call javascript function. I have done it.
    – Ashwani
    Commented Sep 5, 2013 at 13:25
3

Here is a sleep workaround I've used within Apex:

Long startingTime = System.now().getTime(); // Num milliseconds since Jan 1 1970
Integer delayInMilliseconds = 1000; // One-second delay
while (System.now().getTime() - startingTime < delayInMilliseconds)  {
        // Do nothing until desired delay has passed
}

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