Javascript 尝试向jQuery AJAX请求添加延迟

Javascript 尝试向jQuery AJAX请求添加延迟,javascript,jquery,ajax,delay,onkeyup,Javascript,Jquery,Ajax,Delay,Onkeyup,我试图延迟AJAX请求,以便在输入单元格的最后一次键控后2-3秒发送该请求。 到目前为止,我已经成功地延迟了请求,但在2-3秒后,我收到了一个针对字段中每个键控的请求… 如何让jQuery取消前几个键,只发送最后一个键? 以下是迄今为止的代码: $('#lastname').focus(function(){ $('.terms :input').val(""); //clears other search fields }).keyup(function(){ ca

我试图延迟AJAX请求,以便在输入单元格的最后一次键控后2-3秒发送该请求。
到目前为止,我已经成功地延迟了请求,但在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请求。 我只想在最后一次按键后发送一个 我在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);
                }
        }

将超时存储在变量中,以便能够清除最近的超时:

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

        }, 3000);

我喜欢Molle的答案,但我希望进一步提高性能

var ajaxRequest2;  // The variable that makes Ajax possible!
function getAjaxObject()
{
                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;
                     }
                  }
                }
  return ajaxRequest2;


 }
   getAjaxObject();

    function ajaxSearchRequest($type){



               if(typeof ajaxRequest2 =="undefined" || ajaxRequest2 == false)
                {
                  return;
                }
               ajaxRequest2.abort();

                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);
                }
        }
此更改将中止正在进行的ajax请求并发送新的请求。当你


键入->等待4秒->发送请求->再次键入(未收到响应)->等待4秒->另一个请求触发

您尝试执行的操作称为去抖动

这是本·阿尔曼写的一本书

并且还包括此功能


没有必要破解ajax请求系统。只需确保在正确的时间调用它。

发布
ajaxSearchRequest
@Imdad的代码为什么需要这样做?您已经可以看到每个键合都在进行新的超时,而不取消以前的超时。还有其他解决方法。我只能说,如果我知道什么功能是有一种方法来取消延迟事件上。按键?因此,如果没有进一步的keydown,keyup事件将实际发生?@krasatos解决方案是在设置新超时时,简单地取消以前的(
.cleartimout
)超时。那么最后一个keyup超时就不会被取消。当你不将该值存储在全局变量中时,我会给你一个+1。thanx很多,它工作了,最后的代码是:`$('#lastname').focus(function(){$('.terms:input').val(“”;')).keyup(function(){caps(this);$type=$(this.attr(“id”);window.timer=setTimeout(function(){//为每个按键设置延迟ajaxSearchRequest($type);//运行ajax请求},3000);})。keydown(function(){clearTimeout(window.timer);});`ffs这是示例代码。解决方案不需要一个闭包。因为这是一个错误的答案?中止请求没有帮助-服务器(通常)继续处理查询。正确的解决方法不是继续发送请求。但是,这样可以减少网络开销。并且可以解决在键入速度有点慢的情况下获取多个响应的问题。不发送请求“减少网络开销”.取消AJAX请求只会阻止最终答案到达回调。我非常感谢引用您的论点,即“中止不会减少网络开销,只会停止回调接收数据”通常情况下,不发送请求比发送请求但中止请求的网络开销更小。