-
-
Save thebiltheory/9b0fb1e473b5c2e8594c96b1fadd65e3 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