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
Post a Comment