How to delay the .keyup() handler until the user stops typing?

前端 未结 27 2599
半阙折子戏
半阙折子戏 2020-11-21 23:32

I’ve got a search field. Right now it searches for every keyup. So if someone types “Windows”, it will make a search with AJAX for every keyup: “W”, “Wi”, “Win”, “Wind”, “Wi

27条回答
  •  时光说笑
    2020-11-21 23:57

    Well, i also made a piece of code for limit high frequency ajax request cause by Keyup / Keydown. Check this out:

    https://github.com/raincious/jQueue

    Do your query like this:

    var q = new jQueue(function(type, name, callback) {
        return $.post("/api/account/user_existed/", {Method: type, Value: name}).done(callback);
    }, 'Flush', 1500); // Make sure use Flush mode.
    

    And bind event like this:

    $('#field-username').keyup(function() {
        q.run('Username', this.val(), function() { /* calling back */ });
    });
    

提交回复
热议问题