Попытка добавить задержку в запрос JQuery AJAX

Я пытаюсь отложить запрос AJAX, чтобы он отправлялся через 2-3 секунды после нажатия клавиши LAST в ячейке ввода.
До сих пор мне удавалось задерживать запросы, но через 2-3 секунды я получаю по одному запросу на каждый запрос в поле ...
Как я могу заставить jQuery отменить первые и просто отправить последний брелок?
Вот этот код:

$('#lastname').focus(function(){
          $('.terms :input').val(""); //clears other search fields
}).keyup(function(){
    caps(this); //another function that capitalizes the field
    $type = $(this).attr("id"); // just passing the type of desired search to the php file
        setTimeout(function(){ // setting the delay for each keypress
                ajaxSearchRequest($type); //runs the ajax request

        }, 1000);
});

Этот код выше, ждет 1 секунду, затем отправляет 4-5 AJAX-запросов в зависимости от нажатия клавиш. Я просто хочу, чтобы один отправил после последнегоkeyup
Я нашел несколько похожих решений в StackOverflow, которые используют Javascript, но я не смог реализовать их в своем проекте из-за своих небольших знаний в программировании.

[Решено] Окончательный рабочий код, благодаря @ Dr.Molle:

$('#lastname').focus(function(){
          $('.terms :input').val("");
}).keyup(function(){
    caps(this);
    $type = $(this).attr("id");

    window.timer=setTimeout(function(){ // setting the delay for each keypress
            ajaxSearchRequest($type); //runs the ajax request

        }, 3000);

}).keydown(function(){clearTimeout(window.timer);});

ЗдесьajaxSearchRequest код:

function ajaxSearchRequest($type){
                var ajaxRequest2;  // The variable that makes Ajax possible!

                try{
                  // Opera 8.0+, Firefox, Safari
                  ajaxRequest2 = new XMLHttpRequest();
                }catch (e){
                  // Internet Explorer Browsers
                  try{
                     ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
                  }catch (e) {
                     try{
                    ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP");
                     }catch (e){
                    // Something went wrong
                    alert("Browser error!");
                    return false;
                     }
                  }
                }

                ajaxRequest2.onreadystatechange = function(){
                  if(ajaxRequest2.readyState == 4){

                        $result = ajaxRequest2.responseText;
                        $('#resultcontainer').html($result);

                    }}


                var searchterm = document.getElementById($type).value;


                var queryString ="?searchterm=" + searchterm +"&type=" +$type;


                if(searchterm !== ""){

                ajaxRequest2.open("GET", "searchrequest.php" + 
                                 queryString, true);
                ajaxRequest2.send(null);
                }
        }

Ответы на вопрос(3)

Ваш ответ на вопрос