按键时,X秒后停止通话功能 [英] On keypress, when stop after X seconds call function
问题描述
我有一个文本输入和一个textarea,并将值从输入传递到textarea.我想做的是,当您在输入中键入内容并停止时,请在2秒钟后显示文本区域的值.
I have a text input and a textarea and I'm passing the value from the input to the textarea. I am trying to do, when you type something in the input and you stop, after 2 seconds show the values to the textarea.
在此示例中,textarea立即获取输入的值:
In this example the textarea gets the input's value instantly:
因此,我想在2秒钟后键入并停止输入.
我该如何实现?我尝试使用setTimeout
,但是经过2秒钟后,它一直保持立即获取值.因此,基本上,它可以在前2秒内正常工作.
How can I achieve this? I tried to use setTimeout
but when the 2 seconds pass, then it keeps getting the value instantly. So basically it works for the first 2 seconds.
推荐答案
每次用户再次按下该键时,您都必须重置计时器:
You have to reset the timer everytime the user presses the key again:
jQuery(function($){
function changeFn(){
alert('Changed');
}
var timer;
$("#string").bind("keyup", function(){
clearTimeout(timer);
timer = setTimeout(changeFn, 2000)
});
});
这篇关于按键时,X秒后停止通话功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!