Call a javascript function after 5 sec of last key press

前端 未结 3 627
-上瘾入骨i
-上瘾入骨i 2020-12-08 01:03

I have a form with an and I want to call a javascript function after 5 seconds of the last key press, and every time a new key is pres

3条回答
  •  渐次进展
    2020-12-08 01:33

    Something like this should get you started:

    var timeout;
    $('input[type=text]').keypress(function() {
        if(timeout) {
            clearTimeout(timeout);
            timeout = null;
        }
    
        timeout = setTimeout(myFunction, 5000)
    })
    

提交回复
热议问题