Delay ajax call on textfield to allow for typing I would like to delay ajax from firing in the same way that the autocomplete seems to work. For example, if a user it typing, the ajax will not run until 500ms has elapsed since the last keyup.

I am currently looking at drupal.behaviors but not able to make it work. 

Drupal.behaviors.mymodule = {
  attach: function(context, settings) { 
    $('input.andtimer', context).delay(500).ajaxStart();
  }
};


This is the form element to which the behavior is attached.

$form['my_input'] = array(
  '#type' =&gt; 'textfield',
  '#default_value' =&gt; $value,
  '#ajax' =&gt; array(
    'callback' =&gt; 'my_callback',        
    'event' =&gt; 'keyup',
    'wrapper' =&gt; 'my_wrapper',  
    'trigger_as' =&gt; array(
      'name' =&gt;  'my_button',
  ),
  'progress' =&gt; array('type' =&gt; 'none'),
  ),
  '#attributes' =&gt; array(
    'class' =&gt; array('andtimer'),
  ),                      
);


This jsfiddle shows what I am trying to achieve.

Would How to override Drupal.ajax.prototype.beforeSend? be a route to take this down?

The following works for the first 'set' of inputs with the class .andtimer. It is not working for any other set, the ajax always continues with the first set. Any ideas how to fix this?

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


Using $form['my_input']['#ajax']['event'] = 'finishedinput' as suggested and 

var typingTimer;
var doneTypingInterval = 600;

$('.andtimer').on('keyup', function (e) {
  clearTimeout(typingTimer);
  if ($(this).val) {
    var trigid = $(this);
    typingTimer = setTimeout(function(){                    
      trigid.triggerHandler('finishedinput');
    }, doneTypingInterval);
  }
});


Works for each 'group' of inputs where the count of filled inputs needs to be obtained.
One option is to use a custom jQuery event, eg. something like finishedinput. Set $form['my_input']['#ajax']['event'] = 'finishedinput' and provide some JS to trigger your custom event after an appropriate delay (similar to the JS in the fiddle).