开发者

jQuery keyup event needs pause between keystrokes

开发者 https://www.devze.com 2023-04-02 06:57 出处:网络
I have an ajax method that fires on the keyup event, but I don\'t really want it to fire instantly, every single time the user presses a key - ideally I\'d like to give a 1-2 second delay before going

I have an ajax method that fires on the keyup event, but I don't really want it to fire instantly, every single time the user presses a key - ideally I'd like to give a 1-2 second delay before going off to the server, so if the user presses a key every 1/2 second, there will only be 1 call t开发者_开发问答o the server when they finish typing (after 1-2 seconds of not typing anything).

How can I achieve this using jQuery?


var timer = null;
$("#element").keyup(function(){
    clearTimeout(timer);
    timer = setTimeout(function(){
        //do your stuff
    }, 2000);
});


var to;
$("elm").bind("keyup",function(){
  clearTimeout(to);
  to = setTimeout(function(){$.post()}, 3000);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消