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' => 'textfield',
'#default_value' => $value,
'#ajax' => array(
'callback' => 'my_callback',
'event' => 'keyup',
'wrapper' => 'my_wrapper',
'trigger_as' => array(
'name' => 'my_button',
),
'progress' => array('type' => 'none'),
),
'#attributes' => array(
'class' => 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.