EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: rezzonico on February 26, 2016, 05:31:31 AM



Title: delay on keypress event
Post by: rezzonico on February 26, 2016, 05:31:31 AM
Hi all,

I have a datagrid with a combogrid field.
On this field and I have a "bind" on a "keypress" event:

Code:
odlEditor.target.combogrid('textbox').bind('keypress', function() {
   $.ajax({
      ...
   });
});

This works.
But to limit the execution of the ajax code I want to add a "delay" to the "keypress" event.
How is it possible ?

Thanks for any help.
Miche


Title: Re: delay on keypress event
Post by: jarry on February 26, 2016, 09:06:16 AM
The popular way to solve this issue is to use 'setTimeout' and 'clearTimeout' functions to delay an action on the 'keypress' event.
Code:
var timer = null;
.bind('keypress', function(){
if (timer){
clearTimeout(timer);
}
timer = setTimeout(function(){
$.ajax({...})
}, 400);
})


Title: Re: delay on keypress event
Post by: rezzonico on February 26, 2016, 09:42:06 AM
It works !

Thanks a lot.
Miche