You use setTimeout
to schedule a javascript function to run sometimes in the future.
setTimeout(hideDiv, 6000);
See MDN for documentation on setTimeout()
. The first argument is a reference to the function you want it to execute. The second argument is milliseconds of delay.
Keep in mind that this will not stop other javascript that comes after this statement from executing. That will continue to execute immediately. It will schedule that particular function to run on it's own at the allotted time in the future. So, this is not like sleep()
in other languages. It does not stop execution for 6 seconds. Instead, it schedules this particular function to run later, but continues executing the rest of your javascript.
So, if you had this code:
setTimeout(hideDiv, 6000);
document.getElementById("hello").style.color = "red";
The color would change immediately and hideDiv()
would be called in 6 seconds.