My goal is to tackle two issues at once: 1) using
setTimeout( #action#, timeMillis)
with #action# as a lambda 2) supplying the lambda with a parameter.
The common method of
setTimeout( ()=>{ #callback# }, timeMillis)
works flawlessly when extracting ()=>{ #callback# }
to lambda (in this case, lambda has no parameters). However, if I attempt to pass a lambda with a parameter to setTimeout, the function malfunctions - the callback triggers immediately.
Below is the code snippet:
let lambda = (text: string): TimerHandler => {
alert(text)
return ""
}
. . . .
lambda('text1') // value passes to lambda (displays 'text1') - OK | no delay - X
setTimeout(lambda, 3000); // value does not pass to lambda (shows 'undefined') - X | delay - OK
setTimeout(lambda('text3'), 3000); // value passes to lambda (displays 'text1') - OK | no delay - X
Note: Per setTimeout documentation, the first parameter #action# is of type TimerHandler, which extends string. Therefore, I return an "empty_string" inside the lambda body.
Could the unexpected behavior be due to the empty string in the return statement? I am unsure what should be set there.