[英]How to check if two input fields are filled in and wait 2 seconds using jquery
[英]How to wait 2 seconds for input in Javascript
我正在使用javascript编程电视遥控器,如果用户再按一次1(1通道或11通道或111通道),则需要按2号功能等待2秒。
我写了这个函数,但它在循环中仅显示11111111
,这是我的代码:
function chChannel(pressed) {
setInterval(function () {
if (lengthCS < 3) {
inputChannel += pressed;
lengthCS = inputChannel.length;
}
}, 2000);
// call function to change channel using variable value inputChannel for ex. 011
};
有什么解决办法吗?
因此,函数需要等待2秒钟,然后等待2秒钟,它会在变量InputChannel中获取按下数字的值,这样我就可以调用函数以将Channel从Value InputChannel更改为数字。
听按键并计数是一本有据可查的东西,但是,在实际主题的背景下,我想说,没有正确的Waiting方法,除了将超时调用设置为可重复检查的函数来结束脚本外,等待时间是否已经过去,如果没有过去,则使用例如setTimeout进行自触发。 100ms,如果是,则调用继续主程序的函数。 然后,等待线程还可以检查和计数按键,例如。 在全局变量中作为基本解决方案。
使用“ setTimeout”。
var Timer;
function chChannel(pressed) {
clearTimeout(Timer);
Timer = setTimeout(function () {
if (lengthCS < 3) {
inputChannel += pressed;
lengthCS = inputChannel.length;
}
}, 2000);
// call function to change channel using variable value inputChannel for ex. 011
};
您是否考虑过使用下划线的反跳功能之类的东西,会使事情变得更加简单。
http://underscorejs.org/#debounce
可能是这样的事情可能在电话上无法自己测试...
var inputChannel;
function chChannel(pressed) {
if (inputChannel <= 3) {
inputChannel += pressed;
}
else {
inputChannel = pressed;
}
_.debounce(someFunction, 2000);
};
只要在延迟时间内不断调用防抖功能,它就不会执行,只会在延迟后执行一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.