Transcript from the "setTimeout" Lesson
[00:01:16] [LAUGH] They have to get along because we live in universe. I mean, I don't totally understand the physics of it, but time exists or at least we've all agreed that it exists, and we all have watches and clocks that say that it exists. And so our programs also have to do things over time, running code takes time.
[00:02:34] Or maybe we did some other things that are gonna take a long time, which we can talk about in a minute. And if anybody remembers the theme song to Dawson's Creek, I don't wanna wait for my life to be over before I get to interact with my web browser again.
[00:05:28] And it's gonna keep trying to run the rest of our program. And say, all right, I'm gonna get to that long task later, at some point. And a task like setTimeout is gonna run asynchronously, meaning at a different time than kind of where it appears in the code.
[00:06:40] This will print first, okay, that seems pretty reasonable, then it added our console.log. This will print third to a to-do list somewhere and said, hey, let me come back to this after one second. Then it kept running our program, and it printed the final line of our code, but the second thing to print.
[00:07:31] So what we have to do is text or put the drink down on a table somewhere, then send our text, and then pick the drink back up. And this is so that our browser still gets to do stuff. So we can still click on things, and we can still interact with our web pages, even though we know that there's some other thing that we need to do later that's taking a long time.
[00:08:37] So we wrote the code at a particular point in our program, but that's not necessarily when it's going to execute. It's gonna execute later when something else happens. Other stuff that takes time is asking a user for something, when it says, hey, where do you wanna save this file?
[00:08:56] Or hey, which file do you wanna upload into this website? Or can I have permission to record from your camera? Things like that, those take time. Another thing that takes time that's gonna be relevant for our doggo game is it takes time to load data from somewhere else on the interwebs.
>> How much time do you allow for asynchronous actions?
>> How much time do you allow? Well, it really depends, which is a frustrating answer to almost every question that it is an answer to, which is a lot in computer science. It depends, but for example, for our setTimeout, which we said we want this to wait 1,000 milliseconds or one second.
[00:12:36] It adds this timeout to a task list. But if there's a lot of other stuff on that task list, then it might take it even more than 1,000 milliseconds before it actually gets to run the code. So in terms of how much time we allow, there's not a fixed answer.
[00:12:52] For example, if I am calling up a server on the other side of the planet, how much time that's gonna take for my browser to send a message across the interwebs to somewhere thousands and thousands and thousands of miles or kilometers away, and then get an answer back, that can depend on a lot of things.
[00:13:13] It can depend on what the person who built that website that I'm talking to did on their end, how fast it is, how slow it is. Could depend on whether a shark has not chewed through a trans-oceanic data cable somewhere. It can depend on a lot of things.
[00:13:32] So there's not really a hard and fast answer.