setTimeout(setColor('yellow'),6000);
You are calling setColor('yellow')
and passing the return value (which is undefined
) to setTimeout
.
You need to pass it a function.
It is also important to note that setTimeout
will cause a function to be called after a time. It doesn't make JavaScript sleep for that period.
setTimeout(setColor.bind(window, 'yellow'),6000);
setTimeout(setColor.bind(window, 'red'),6000);
… will call setTimeout
at 0s, then call setTimeout
again a fraction of a second later, then call setColor('yellow')
at 6s and setColor('red')
a fraction of a second after that.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…