I'm trying to create a small function that runs every minute, but if the user is still typing when the function is to be executed, delay the execution until the user stops typing.
I have read this and other relevant questions, but my problem is different in that it includes an already scheduled function execution through setTimeout()
I have tried resetting the timer if the user is still typing, but this only forces the function to run again multiple times.
var timer = null
runTimer()
$('body').on('keyup', ':input', function(e) {
timer = setTimeout(runTimer, 400);
});
$('body').on('keydown', ':input', function(e) {
clearTimeout(timer);
});
function runTimer() {
setTimeout(function(){
executeFunction();
}, 60 * 1000);
}
function executeFunction() {
console.log('Ran function')
setTimeout(function(){
executeFunction();
}, 60 * 1000);
}
I want the function to be executed once every minute, delayed until user stops typing but reset the timer to one minute after being executed. Also using underscore.js
is acceptable.