Delay ajax keyup event while user is typing similar to autocomplete

No replies
djsherlock
Offline
Last seen: 7 years 39 weeks ago
Joined: 2014-03-29

I am looking to produce the same delay functionality that is present when using an autocomplete field, in that there is a delay to allow the user to finish typing (e.g. 300ms) before the ajax call is fired.

I have the ajax within the form working the way I want it to but the 'event' => 'change' causes other issues, so I would like to use the keyup event.

The following code only partially works as there is more than one group of inputs with this ajax attached. It will work for the first group but even when triggered in the second group, the re-invoking of the handler, triggers the first group again.

(function($, Drupal) {
  Drupal.behaviors.bform = {
    attach: function(context, settings) {  
     
       var events = $('.andtimer').clone(true).data('events');
       $('.andtimer').unbind('keyup');    
           var typingTimer;
           var doneTypingInterval = 300;              
           $('.andtimer').keyup(function(){      
               clearTimeout(typingTimer);
       typingTimer = setTimeout(doneTyping, doneTypingInterval);
            function doneTyping() {            
                   $.each(events.keyup, function() {
              this.handler();
            });
                }
              return false;              
           });        
       }
  };
})(jQuery, Drupal);

Does anyone have any solutions to this problem or suggestions for an alternative method?

I have gone throught he misc/autocomplete/js file but it is over my head. I think the key would be in here though,

Any help much appreciated.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.