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

前端 未结 29 2026
我寻月下人不归
我寻月下人不归 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:51

    So, I'm going to guess finish typing means you just stop for a while, say 5 seconds. So with that in mind, lets start a timer when the user releases a key and clear it when they press one. I decided the input in question will be #myInput.

    Making a few assumptions...

    //setup before functions
    var typingTimer;                //timer identifier
    var doneTypingInterval = 5000;  //time in ms, 5 second for example
    var $input = $('#myInput');
    
    //on keyup, start the countdown
    $input.on('keyup', function () {
      clearTimeout(typingTimer);
      typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });
    
    //on keydown, clear the countdown 
    $input.on('keydown', function () {
      clearTimeout(typingTimer);
    });
    
    //user is "finished typing," do something
    function doneTyping () {
      //do something
    }
    
    0 讨论(0)
  • 2020-11-22 04:51

    I feel like the solution is somewhat a bit simpler with the input event:

    var typingTimer;
    var doneTypingInterval = 500;
    
    $("#myInput").on("input", function () {
        window.clearTimeout(typingTimer);
        typingTimer = window.setTimeout(doneTyping, doneTypingInterval);
    });
    
    function doneTyping () {
        // code here
    }
    
    0 讨论(0)
  • 2020-11-22 04:52

    Both top 2 answers doesn't work for me. So, here is my solution:

    var timeout = null;
    
    $('#myInput').keyup(function() {
        clearTimeout(timeout);
    
        timeout = setTimeout(function() {
            //do stuff here
        }, 500);
    });
    
    0 讨论(0)
  • 2020-11-22 04:54

    agree with the @going 's answer. Another similar solution that worked for me is the one below. The only difference is that I am using .on("input"...) instead of keyup. This only captures changes in the input. other keys like Ctrl, Shift etc. are ignored

    var typingTimer;                //timer identifier
    var doneTypingInterval = 5000;  //time in ms (5 seconds)
    
    //on input change, start the countdown
    
    $('#myInput').on("input", function() {    
        clearTimeout(typingTimer);
        typingTimer = setTimeout(function(){
            // doSomething...
        }, doneTypingInterval);
    });
    
    0 讨论(0)
  • 2020-11-22 04:54

    Wow, even 3 comments are pretty correct!

    1. Empty input is not a reason to skip function call, e.g. I remove waste parameter from url before redirect

    2. .on ('input', function() { ... }); should be used to trigger keyup, paste and change events

    3. definitely .val() or .value must be used

    4. You can use $(this) inside event function instead of #id to work with multiple inputs

    5. (my decision) I use anonymous function instead of doneTyping in setTimeout to easily access $(this) from n.4, but you need to save it first like var $currentInput = $(this);

    EDIT I see that some people don't understand directions without the possibility to copy-paste ready code. Here you're

    var typingTimer;
    //                  2
    $("#myinput").on('input', function () {
        //             4     3
        var input = $(this).val();
        clearTimeout(typingTimer);
        //                           5
        typingTimer = setTimeout(function() {
            // do something with input
            alert(input);
        }, 5000);      
    });
    
    0 讨论(0)
提交回复
热议问题