Wednesday, January 6, 2010

Last KeyUp Event (JQuery)....

In some cases while developing web, I had an idea to optimize the validation. I think that was so wasting to check an input when keyup. Why spent many times to check if the result is the same as did it in one last time.
To get the last keyup, it have to delay the check process until the last keyup. To delay some process, we need timers. I prefer to use JQuery.timers, cause i have applying JQuery library before and the timers can be an object on the element.    

{Indonesian Version : http://faridjauhari.wordpress.com/2009/12/31/event-keyup-terakhir/}

This code is an example of my implementation :


$('#some_item_id').keyup(function(){
       // cancel the last counter
       $(this).stopTime();
      // make new counter
      $(this).oneTime(200,function(){
              // put ajax code to check this input below
              $.ajax({
              url         : 'http://mydomain.com/some_page.php?func=some_function&inp='.$(this).val(),
              dataType : 'json',
              success  : function(result){
                                   // the action while success
                            },
              error      : function(){
                                   //the action while error
                           }
              });
      }
});

Try it by yours....
----------- sharing in internet is the nice way to improve -------------------

No comments:

Post a Comment