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

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

    I don't think keyDown event is necessary in this case (please tell me why if I'm wrong). In my (non-jquery) script similar solution looks like that:

    var _timer, _timeOut = 2000; 
    
    
    
    function _onKeyUp(e) {
        clearTimeout(_timer);
        if (e.keyCode == 13) {      // close on ENTER key
            _onCloseClick();
        } else {                    // send xhr requests
            _timer = window.setTimeout(function() {
                _onInputChange();
            }, _timeOut)
        }
    
    }
    

    It's my first reply on Stack Overflow, so I hope this helps someone, someday:)

    0 讨论(0)
  • 2020-11-22 04:36

    I just figured out a simple code to wait for user to finish typing:

    step 1.set time out to null then clear the current timeout when the user is typing.

    step 2.trigger clear timeout to the variable define before keyup event is triggered.

    step 3.define timeout to the variable declared above;

    <input type="text" id="input" placeholder="please type" style="padding-left:20px;"/>
    <div class="data"></div>
    

    javascript code

    var textInput = document.getElementById('input');
    var textdata = document.querySelector('.data');
    // Init a timeout variable to be used below
    var timefired = null;
    
    // Listen for keystroke events
    // Init a timeout variable to be used below
    var timefired = null;// Listen for keystroke events
    textInput.onkeyup = function (event) {
    clearTimeout(timefired);
    timefired = setTimeout(function () {
        textdata.innerHTML = 'Input Value:'+ textInput.value;
      }, 600);
    };
    
    0 讨论(0)
  • 2020-11-22 04:37

    Why not just use onfocusout?

    https://www.w3schools.com/jsreF/event_onfocusout.asp

    If it's a form, they will always leave focus of every input field in order to click the submit button so you know no input will miss out on getting its onfocusout event handler called.

    0 讨论(0)
  • 2020-11-22 04:37

    Once you detect focus on the text box, on key up do a timeout check, and reset it each time it's triggered.

    When the timeout completes, do your ajax request.

    0 讨论(0)
  • 2020-11-22 04:38

    Modifying the accepted answer to handle additional cases such as paste:

    //setup before functions
    var typingTimer;                //timer identifier
    var doneTypingInterval = 2000;  //time in ms, 2 second for example
    var $input = $('#myInput');
    
    // updated events 
    $input.on('input propertychange paste', function () {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(doneTyping, doneTypingInterval);      
    });
    
    //user is "finished typing," do something
    function doneTyping () {
      //do something
    }
    
    0 讨论(0)
  • 2020-11-22 04:39

    I like Surreal Dream's answer but I found that my "doneTyping" function would fire for every keypress, i.e. if you type "Hello" really quickly; instead of firing just once when you stop typing, the function would fire 5 times.

    The problem was that the javascript setTimeout function doesn't appear to overwrite or kill the any old timeouts that have been set, but if you do it yourself it works! So I just added a clearTimeout call just before the setTimeout if the typingTimer is set. See below:

    //setup before functions
    var typingTimer;                //timer identifier
    var doneTypingInterval = 5000;  //time in ms, 5 second for example
    
    //on keyup, start the countdown
    $('#myInput').on("keyup", function(){
        if (typingTimer) clearTimeout(typingTimer);                 // Clear if already set     
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    });
    
    //on keydown, clear the countdown 
    $('#myInput').on("keydown", function(){
        clearTimeout(typingTimer);
    });
    
    //user is "finished typing," do something
    function doneTyping () {
        //do something
    }
    

    N.B. I would have liked to have just added this as a comment to Surreal Dream's answer but I'm a new user and don't have enough reputation. Sorry!

    0 讨论(0)
提交回复
热议问题