简体   繁体   English

如何在第二次触发侦听器事件之前设置延迟

[英]How to set a delay before a listener event is triggered for the second time

I'm testing some code to check if input includes a string.我正在测试一些代码来检查输入是否包含字符串。 It works fine however when I type rapidly it get's triggered twice?它工作正常但是当我快速输入时它会触发两次? Can someone explain why and how I can fix it?有人可以解释为什么以及如何解决它吗?


var inputSelector = document.querySelector('input[name="category"]');

inputSelector.addEventListener('keyup', function(){
    var inputNaam = inputSelector.value;
    inputNaam.toLowerCase();
    if(inputNaam.length === 9 && inputNaam.includes('baardzorg')){
            console.log('match')
    }
})

I think this happens because if I type rapidly I will release the two last keys after I finished typing 'baardzorg'.我认为发生这种情况是因为如果我快速键入,我会在完成键入“baardzorg”后释放最后两个键。 How can I prevent this from happening?我怎样才能防止这种情况发生?

Problem in your code:您的代码中的问题:

When you are typing fast, the two keys ( r and g of baardzorg) tend to be released at the same time ( it doesn't happen in normal case ).当您快速打字时,两个键(r 和 baardzorg 的 g)往往会同时释放(在正常情况下不会发生)。 Pressing and leaving the keys together reproduced the issue.将键一起按下并离开会重现该问题。 There are two keyup events and in both the cases the event matches and console.log is triggered.有两个 keyup 事件,在这两种情况下,事件匹配并触发 console.log。

Solved:解决了:

You can use this concept called debouncing if you are to achieve this using 'keyup'.如果您要使用“keyup”来实现这一点,您可以使用这个称为去抖动的概念。 you need to call the function only when there is delay in typing, exactly saying the user has stopped typing for a few millisecond.These are used in search engines mostly, for not to call an api for each typed word ( for getting frequently searched word or in ecommerce searches to list products), it would cost a lot to call an api for each searched word.只有在输入延迟时才需要调用 function,确切地说是用户已经停止输入几毫秒。这些主要用于搜索引擎,因为不为每个输入的单词调用 api(用于获取经常搜索的单词或在电子商务搜索中列出产品),为每个搜索的单词调用 api 会花费很多。 It will call the api once there is delay in typing.一旦输入延迟,它将调用 api。 Please see the code below:请看下面的代码:

 var inputSelector = document.querySelector('#fname'); const findMatch = () => { var inputNaam = inputSelector.value; inputNaam.toLowerCase(); if(inputNaam.length === 9 && inputNaam.includes('baardzorg')){ console.log('match') } } const debounce = function (fn, d) { let timer; return function () { clearTimeout(timer); timer = setTimeout(() => { findMatch.apply(); }, d); } } const onFinishTyping = debounce(findMatch, 300);
 <input type="text" id="fname" onkeyup="onFinishTyping()" name="fname"><br><br>

You should use onChange or onBlur instead of keyup event.您应该使用onChangeonBlur而不是keyup事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在设置侦听器之前会触发事件吗? - Will event be triggered before I set up the listener? 事件侦听器目标是 null 延迟后设置超时 - Event listener target is null after delay with set time out 我可以在第二次触发事件时执行不同的代码集吗? - Can I execute a different set of code on the second time an event is triggered? 如何防止事件监听器 function 被同时触发多次 - how to prevent a event listener function to be triggered several times at the same time JQuery:在定义事件侦听器之后但在触发侦听器之前删除JS - JQuery: Remove JS after defining event listener but before listener is triggered 如果在设置侦听器之前触发了DOMContentLoaded事件,则如何检索“事件”对象 - How to retrieve the `event` object if the DOMContentLoaded event has been triggered before setting a listener 在执行JavaScript之前设置5秒延迟 - Set 5 second delay before executing javascript 如果之前没有使用jQuery设置过,如何绑定事件监听器? - How to bind event listener if not previously set before with jQuery? 单击事件侦听器仅在第二次工作 - Click event listener works just on the second time 如何在每次第二次事件发生时在事件监听器中执行函数? - How to execute function in event listener only every second time event is happening?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM