Last active
          June 11, 2016 09:13 
        
      - 
      
- 
        Save rponte/5784478 to your computer and use it in GitHub Desktop. 
    Delaying actions on keypress with jQuery
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | $('#mySearch').keyup(function() { | |
| clearTimeout($.data(this, 'timer')); | |
| var wait = setTimeout(search, 500); | |
| $(this).data('timer', wait); | |
| }); | |
| function search() { | |
| $.post("stuff.php", {nStr: "" + $('#mySearch').val() + ""}, function(data){ | |
| if(data.length > 0) { | |
| $('#suggestions').show(); | |
| $('#autoSuggestionsList').html(data); | |
| }else{ | |
| $('#suggestions').hide(); | |
| } | |
| }); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | $('#searchString').keyup(function(e) { | |
| clearTimeout($.data(this, 'timer')); | |
| if (e.keyCode == 13) | |
| search(true); | |
| else | |
| $(this).data('timer', setTimeout(search, 500)); | |
| }); | |
| function search(force) { | |
| var existingString = $("#searchString").val(); | |
| if (!force && existingString.length < 3) return; //wasn't enter, not > 2 char | |
| $.get('/Tracker/Search/' + existingString, function(data) { | |
| $('div#results').html(data); | |
| $('#results').show(); | |
| }); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
More informations,
http://stackoverflow.com/questions/2410937/delaying-actions-between-keypress-in-jquery
http://stackoverflow.com/questions/3028704/optimized-search-using-ajax-and-keypress