setTimeout(pulse(), 4000);
should be setTimeout(pulse, 4000);
Notice there are no parentheses after pulse
.
This is because when the parentheses are present, it means the function should be evaluated (which is what causes it's immediate execution). Without them, it is a reference to a function passed as an argument to setTimeout
and will only be executed when setTimeout
chooses to execute it, which is what you want.