On 10 Sep 2007, at 10:44, byronrode wrote:

> I have an input form that I am using for a live search feature, and
> currently rely on a minimum keypress activation for the ajax call.
>
> i want to have the ajax call initiated once the user has stopped
> typing for around 2 secs. so basically 2 seconds after their final
> keypress then the ajax call gets initiated.
>
> Is this possible, and please dont send ready made coed, I want to
> learn how to do it, so if you can point me in the right direction or
> to a function that i can use to integrate this into my script I would
> be greatful.
>
> I have tried searching, but I think I am using the wrong terminology.

Script.aculo.us extends Form.Element with a DelayedObserver in the  
controls.js file:

// Delayed observer, like Form.Element.Observer,
// but waits for delay after last key input
// Ideal for live-search fields

Form.Element.DelayedObserver = Class.create();
Form.Element.DelayedObserver.prototype = {
   initialize: function(element, delay, callback) {
     this.delay     = delay || 0.5;
     this.element   = $(element);
     this.callback  = callback;
     this.timer     = null;
     this.lastValue = $F(this.element);
     Event.observe 
(this.element,'keyup',this.delayedListener.bindAsEventListener(this));
   },
   delayedListener: function(event) {
     if(this.lastValue == $F(this.element)) return;
     if(this.timer) clearTimeout(this.timer);
     this.timer = setTimeout(this.onTimerEvent.bind(this), this.delay  
* 1000);
     this.lastValue = $F(this.element);
   },
   onTimerEvent: function() {
     this.timer = null;
     this.callback(this.element, $F(this.element));
   }
};

You should be able to use it out-of-the-box if you load it.

Best regards

Peter De Berdt


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to