This is the code from another thread. It activates a function only when the user has stopped typing after a set time.
var keyupTimer;
function keyUpEvent(){
clearTimeout(keyupTimer);
keyupTimer = setTimeout(sendInput,1000); // will activate when the user has stopped typing for 1 second
}
function sendInput(){
alert("Do AJAX request");
}
It works as is. But why does it stop working if I put parenthesis to try to pass variables in this line:
keyupTimer = setTimeout(sendInput,1000); //original code
To
keyupTimer = setTimeout(sendInput(),1000); //with just empty ()
or
keyupTimer = setTimeout(sendInput(var),1000);//or with ('test') or with (var)
with the parenthesis, the delay does not occur and the sendInput function is called immediately. Is this the only format for this particular routine?
TIA
you can try
so have an anonymous function as parameter for ‘setTimeout’