1

I have a javascript remoting method which is called on keypress in an input box, but when the new call is made, is it possible to abort the previous processing? Please let me know if we can achieve this

  • 3
    Hi Ritzy, welcome to SFSE! Please take the time to visit the Help center and read How do I ask a good question. The more details you provide, particularly code you've written, the more likely it is that someone will respond to your question with an answer you'll find helpful. – crmprogdev Apr 1 '16 at 12:42
  • Ritzy have you already gone through the documentation ? Adding such details will help others to get more insight into your questions. When I last used js remoting I didn't think this was possible, but maybe there are clever JS ways to acomplish this. – Samuel De Rycke Apr 1 '16 at 13:50
  • You may have to build your own rest service, the you will be able to abort previous call. – erkan çipil Apr 1 '16 at 13:56
  • Not technically possible but it does buffer calls made close together "JavaScript remoting optimizes requests that are executed close to each other in time and groups the calls into a single request. This buffering improve the efficiency of the overall request-and-response cycle, but sometimes it’s useful to ensure all requests execute independently." so it may be a mute point: developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – Eric Apr 1 '16 at 17:21
1

I don't know if there's a way to abort a call that has already been made but one way I would handle something like this is to utilize the Js 'setTimeout' function. Here is my take:

<input name='test' onkeypress='handleCalls(event)'/>
<script>
  function handleCalls(e){
    if(typeof(timeoutId) !== 'undefined')
      window.clearTimeout(timeoutId);

    timeoutId = window.setTimeout(function(){
      // Your Js Remoting call here
    }, 500);
  }
</script>

This will wait at least 500ms after a keypress to make the remoting call, and reset the timer on each keypress. You can adjust the timer to be faster or slower without affecting the behavior. Just note that JS timing is not very reliable in that respect.

Hope this helps!

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.