Once, a Bit Later
You put a pizza in the oven, and set a timer for 12 minutes. Then you go and do something else. When the timer rings, you take the pizza out. You didn't stand and look at the oven: the timer told you when. JavaScript has timers too.
setTimeout
setTimeout runs a function once, later. It takes the function, and how long to wait, in milliseconds:
console.log("Pizza in the oven");
setTimeout(() => {
console.log("Pizza is ready!");
}, 3000);
console.log("Laying the table");
Pizza in the oven
Laying the table
Pizza is ready!
3000 milliseconds are 3 seconds. Look at the order: "Laying the table" comes before "Pizza is ready!". setTimeout doesn't stop the script to wait. It sets the timer, and goes on at once with the next line. Three seconds later, the function runs.
On the page
The function can change the page, like any other function:
<p id="note">Wait for it...</p>
<script>
setTimeout(() => {
document.querySelector("#note").textContent = "Surprise!";
}, 2000);
</script>
The page says "Wait for it...", and two seconds later, "Surprise!". Change the code a little, and the preview starts again from the beginning: wait the two seconds again.
A function, not a call
setTimeout needs the function itself, so it can call it later. A function with a name works too, written without brackets:
const ring = () => {
console.log("Ring!");
};
setTimeout(ring, 1000);
Break it on purpose
Give it the function with brackets: setTimeout(ring(), 1000). "Ring!" comes at once, not after a second. ring() calls the function right there, and setTimeout gets what it returns, which is nothing. It's the same mistake as with addEventListener: give the function, don't call it.
Then write the time in seconds: setTimeout(ring, 2). It rings at once again: 2 milliseconds is too short to see. The time is always in milliseconds.
Mission: Live data
What you will see: when the page opens, under the last update, it says:
Loading live data...
Two seconds later, the same line says:
Live data loaded
The dashboard gets its numbers from the station's computers, which takes a moment. The crew should see that something is happening.
- Under
#last-update, add a paragraph with the idlive-status. - It says
Loading live data...when the page opens. - Two seconds after the page opens, the script changes it to
Live data loaded, once.