如何在按键时停止超时功能内的事件



已经4个小时了,之后我仍在处理多个警报使用超时。用例是非常简单的

1-用户在输入字段中输入一些值(带有是一个数字字段(。

2-然后,一旦最终用户停止输入号码。我需要用于计算的文本字段值。

3-然后根据一些情况,我需要显示一些警告信息。

问题-我在使用超时后收到2个tice警报,我不知道为什么需要任何帮助

$('#0639').on('keydown', 'input', function(e){
clearTimeout(ty);
var that = $(this)
var ty = setTimeout(function(){
if(that) {
console.log(that.val()) // This is work inside timeout only
alert('true'); //This should alway be once but it's keep showing.
//This is not working inside the timeout. 
e.stopImmediatePropagation();
}
},1000);
})

var ty; 
$('#0639').on('keydown', 'input', function(e){
if (ty) {
clearTimeout(ty);
}
var that = $(this)
ty = setTimeout(function(){
if(that) {
alert('true');
console.log($(this).val()) // This is work inside timeout only
e.stopImmediatePropagation();
}
},1000);
})

最新更新