I currently have a Modal popping up and want it to disappear if the user doesn't input their password for 30 seconds.
This works, but when I tried to implement it with an onChange
event on the input then it stopped working. The weird part is, when I use console.log
to test it, it works. Once I remove those console.log
's, it doesn't work. Just curious if anyone has seen this before?
let TIMEOUT = null;
const setModalTimeout = randomFunction => {
if (TIMEOUT === null) {
// console.log('NOTE: set timeout before', TIMEOUT, new Date().toLocaleTimeString());
TIMEOUT = setTimeout(() => randomFunction(false), MODAL_TIMEOUT);
}
// console.log('NOTE: set timeout after', TIMEOUT, new Date().toLocaleTimeString());
};
const clearModalTimeout = (done) => {
if (TIMEOUT !== null) {
// console.log('NOTE: clear timeout before', TIMEOUT, new Date().toLocaleTimeString());
clearTimeout(TIMEOUT);
TIMEOUT = null;
}
// console.log('NOTE: clear timeout after', TIMEOUT, new Date().toLocaleTimeString());
done && done();
};
const resetModalTimeout = (randomFunction) => {
// console.log('NOTE: reset timeout', TIMEOUT, new Date().toLocaleTimeString());
clearModalTimeout();
setModalTimeout(randomFunction);
}
Above is the logic, below is the input tag.
<input type="password" placeholder="pin/password" id="password" oncreate={element => element.focus()} onchange={() => resetModalTimeout(randomFunction)} />
The main part of this issue is, if I uncomment the console.log
s, it works perfectly. The way the code is shown now, it does not work. It will only go through the First cycle (so only work for 30 seconds even though I make a change to the input). and when I log, it clearly shows that change goes through. Any ideas? Of course, I don't want the console.log
in the main code, also I am using hyperapp (existing code base).
When Logging, I can see the TIMEOUT
value changing as expected.