簡體   English   中英

最后按鍵事件?

[英]Last key pressed event?

如何知道用戶是否停止輸入?

像這樣:

$('#input').onkeyup(function(){
   //do something
});

但是,您如何知道那是否是最后一個.onkeyup事件?

我認為這需要一個計時器,以便知道用戶何時停止鍵入可以在最后一個.onkeyup之后2秒...類似的事情。

想法?

是的,您是對的...您可以使用基於計時器的解決方案,例如

jQuery(function ($) {
    //to store the reference to the timer
    var timer;
    $('#myinput').keyup(function () {
        //clear the previous timer
        clearTimeout(timer);
        //create a new timer with a delay of 2 seconds, if the keyup is fired before the 2 secs then the timer will be cleared
        timer = setTimeout(function () {
            //this will be executed if there is a gap of 2 seconds between 2 keyup events
            console.log('last')
        }, 2000);
    });
})

演示: 小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM