Run javascript function when user finishes typing instead of on key up?

前端 未结 29 2041
我寻月下人不归
我寻月下人不归 2020-11-22 04:03

I want to trigger an ajax request when the user has finished typing in a text box. I don\'t want it to run the function on every time the user types a letter because that wo

29条回答
  •  广开言路
    2020-11-22 04:32

    var timer;
    var timeout = 1000;
    
    $('#in').keyup(function(){
        clearTimeout(timer);
        if ($('#in').val) {
            timer = setTimeout(function(){
                //do stuff here e.g ajax call etc....
                 var v = $("#in").val();
                 $("#out").html(v);
            }, timeout);
        }
    });
    

    full example here: http://jsfiddle.net/ZYXp4/8/

提交回复
热议问题