I wrote an autocomplete plugin for my website, which takes a file path and and the search data and then does a post to get results. The thing I realized is that I'm posting on every keyup of the input, and that's not good. So I thought add a timeout that would only do the search 1 second (or maybe .5 sec) after the last keyup. Unfortunately, it seems that setTimeout can't find my function, and I'm not sure why. The specific error in FireBug is "ReferenceError: search is not defined'. If I don't use the timeout, search gets called just fine. Some help figuring this out (seems like a scope issue) would be nice.

Here's the code (pruned down, there's more) that I'm trying to get working. The search function and the keyup are the important parts. Once I can get this working, I'll move the stuff just above the setTimeout into the function (assuming the variables pass ok). Obviously I have to assign setTimeout to a variable so I can overwrite it and only call it once after the time passes.

jQuery.fn.autocomplete = function (pathOption, sendData) {
	function search() {

	var $inputBox = $(this), onWrapper = false;
	$inputBox.keyup(function () {
		if ($(this).val().length >= 3 && $(this).val() != $(this).data('placeholder')) {
			$.extend(sendData, { search: $(this).val() });
			$.post(SITEROOT + pathOption, sendData, function (data) {
				if (data.length > 0) {
				} else $resultsDiv.slideUp();

			setTimeout('search', 1000);
		} else $resultsDiv.slideUp();