PDA

View Full Version : Cancelling Ajax request



webfriend13
26 Apr 2012, 1:01 AM
Hi All,

I have a text box. On every key down (as soon as user press any key), I fire an ajax request. This textbox basically serach for user name. So if I enter 'a', an ajax request will be initiated to get all the user who's name start with 'a'. So, if user comes and type 'abcd', system will fire 4 ajax request. one for letter 'a', one for letter 'b' and so on. The problem is it may happen that the first request gets the response last and system show the result for that response. I see two possible solutions:
1) Abort the old request.
2) wait for 200 odd millisecods before request is made. In this way, a coorect query will be formed each time.

Please suggest how to implement both solutions.

Thanks in advance

webfriend13
26 Apr 2012, 1:30 AM
Hi All,

While the below solution does not cancle the Ajax request, it does not fire multiple request.

I have added buffer config. It actually delays the function excution by some miliiseconds. So instead of firing multiple request system now makes only one request.

http://www.sencha.com/forum/showthread.php?17957-How-to-add-the-onkeyup-event-to-a-TextField-object

new Ext.form.TextField({
emptyText:'insert name',
listeners: {
'keyup': {
fn: function(obj) {
alert('test');
},
buffer: 1000
}
}
}),



* delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.
* buffer {Number} Causes the handler to be scheduled to run in an Ext.util.DelayedTask delayed by the specified number of milliseconds. If the event fires again within that time, the original handler is not invoked, but the new handler is scheduled in its place.