I am looking for a solution to call a method that posts an answer after an input change in my Angular project. I want to reset the timeout if another input change occurs to avoid multiple posts. Is there a smart way to achieve this?
My project involves posting answers for various questions so it is crucial that the code does not cancel the wrong timeout.
var calls = {};
function postAnswer(answer) {
if(calls[answer.id]) {
clearTimeout(calls[answer.id]);
console.log('cleared ' + answer.id);
}
calls[answer.id] = setTimeout(() => {
console.log("posted " + answer.id)
delete calls[answer.id];
}, 2000)
}
I have been exploring different methods and libraries like RxJS, but haven't found a suitable one yet. Any suggestions would be greatly appreciated.