javascript - function in setTimeout doesn't work -


this question has answer here:

there 2 functions hello1() , hello2().

function hello1(){     console.log('hello1'); }  function hello2(){     console.log('hello2'); }  settimeout(hello1, 3000); settimeout(hello2(), 3000); 

in settimeout(hello1, 3000);, print "hello1" after delay 3 seconds.

but in settimeout(hello2(), 3000);, print "hello2" immediately.

i think it's because must use function name in settimeout.

what if want execute function parameters after delay 3 seconds hello(1)?

because want deliver parameters function can't use function name in settimeout settimeout(hello1, 3000);

when use parenthesis function in settimeout executed immediately.

to use function parameters, can use anynomous function timeout function , call function inside it.

settimeout(function() {     hello(1, 'param'); }, 3000); 

Comments

Popular posts from this blog

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -