Ajax callback called on textfield typing I'm trying to reach exactly the same state as autocomplete field does. When user is typing in textfield I'd like ajax to wait with its callback so the user can write all the text.

I'm trying to achieve it with help of this question. But I'm failing. Maybe because I'm playing with JavaScript in drupal for the first time. Anyway here is my code.

form_alter in .module (I have form generated with features)

if (empty($form['nid']['#value'])) {

    drupal_add_js(drupal_get_path('module','MY_MODULE') . 'my_script.js');

    $form['field_my_field']['und'][0]['value']['#ajax'] = array(
        'callback' =&gt; '_test_callback',
        'event' =&gt; 'finishedinput',
    );
}


my_script.js (from the link above)

(function ($, Drupal) {
Drupal.behaviors.MY_MODULE = {
    attach: function (context, settings) {
      var typingTimer;
      var doneTypingInterval = 10;

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


And I have script attached to my .info file.
here my solution:

first you've to create (or alter) a text field

$form['user_search'] = array(
  '#type' =&gt; 'textfield',
  '#title' =&gt; t('Search'),
  '#description' =&gt; t('Cool description'),
  '#ajax' =&gt; array(
    'callback' =&gt; 'MODULE_ajax_user_search',
    'wrapper' =&gt; 'user-search-replace',
    'event' =&gt; 'finished_input', // custom event called by js code
    'effect' =&gt; 'fade',
  ),
  '#attributes' =&gt; array(
    'class' =&gt; array(
      'delayed-search-submit' // custom class to attach behavior to
    ),
    'data-delay' =&gt; '300', // custom property to set submit delay
    //'data-event' =&gt; 'finished_input', // custom event name, default is "finished_input"
  ),
);


then you'll have to create some js code to attach to your form

(function($){
  $(function(){
    Drupal.behaviors.delayedSearchSubmit = {
      attach: function (context, settings) {
        $('input.delayed-search-submit').once('event', function (){
          var $self = $(this);
          var timeout = null;
          var delay = $self.data('delay');
          var triggerEvent = $self.data('event');
          if (!delay) {
            delay = 1000;
          }
          if (!triggerEvent) {
            triggerEvent = 'finished_input';
          }
          // Every time a keyup event is fired
          // timer is reset
          $self.bind('keyup', function(){
            clearTimeout(timeout);
            timeout = setTimeout(function(){
              $self.trigger(triggerEvent);
            }, delay);
          });
        });
      }
    }
  });
})(jQuery);


now attach js file to your form

drupal_add_js(drupal_get_path('module', 'MODULE') . '/js/MODULE.textsearch.js');