I have one HTML input field
,I want when a user start typing in this field functionA()
should be called ,and wait for 10seconds
,and after 10seconds
if user is still typing then again functionA() should be called ,otherwise functionB
should be called. How can I achieve this in JavaScript
CodePudding user response:
This could help for the first part of your problem. Please show us the code you have tried so that we can understand your question better and help.
document.querySelector("your_input_id").addEventListener("keyup", function() {
setTimeout(function() {
functionA();
}, 10000);
});
CodePudding user response:
if user coninuesly typing then it will call functionA and if user pause typing for 10 seconds and start typing again. it will go to functionB
let timer = null;
let timeOut = false;
const onChange = (e) => {
if(timeOut) {
functionB(e)
return;
} else {
clearTimeout(timer)
timer = setTimeout(()=> timeOut = true, 10000)
functionA(e)
}
}
const functionA = (e) => {
console.log('A', e)
}
const functionB = (e) => {
console.log('B', e)
}
<input onKeyUp="onChange(this.value)"/>