[英]keyup only works one time
$("#hi").keypress(function() {
$(".hi").html("Writing...");
});
$('#hi').keyup(function() {
$(".hi").delay(1000).queue(function() {
$(this).html("");
});
});
當我在文本框(#hi)中鍵入“ Hello”時,鍵入內容僅適用於第一個字母,然后它將消失。
您可以使用超時而不是延遲:
var timeout;
$("#hi").keypress(function() {
// Clear any previous timeout
clearTimeout(timeout);
// Apply the writing text
$(".hi").html("Writing...");
// Remove the text after one second if no more key is pressed until then
timeout = setTimeout(function () {
$(".hi").html("");
}, 1000);
});
這是一個工作示例: http : //jsfiddle.net/tF7DH/1/
這里的想法是您設置一個超時時間,以在擊鍵一秒鍾后刪除“ Writing ...”文本。 如果在該秒內做出了新的按鍵操作,則將清除先前的超時並設置一個新的按鍵。 這樣,僅當用戶停止鍵入一秒鍾以上時,才會刪除文本。
您通過不叫next
來阻塞隊列
從jQuery 1.4開始,被調用的函數將另一個函數作為第一個參數傳遞。 調用時,這將自動使下一個項目出隊並保持隊列移動。 我們按如下方式使用它:
$("#test").queue(function(next) {
// Do some stuff...
next();
});
我不太確定為什么有幫助,但是清除隊列將為您帶來預期的結果
$('#hi').keyup(function() {
console.log("keyup");
$(".hi").delay(1000).queue(function() {
console.log("erase");
$(this).html("").clearQueue();
})
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.