Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add section on timers #1201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions common-content/en/module/js2/timing/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
+++
title = '⏰ Timers'
time = 20
facilitation = false
emoji= '🧩'
[objectives]
1='Explain how we can call functions back after a set amount of time'
2='Describe how to use setTimeout'
3='Define a callback'
[build]
render = 'never'
list = 'local'
publishResources = false
+++

To update the DOM, we'll need to understand the idea of timers and {{<tooltip title="callbacks">}}A callback function is a function that is passed as an argument to another function and gets executed after the main function has finished its execution.{{</tooltip>}}.

```js
function printMessage(name) {
console.log(`My name is ${name}`);
}

printMessage("Sally");
printMessage("Daniel");
```

In this example, we have 2 different parts: **function declaration** and **function calls**. We define the function `printMessage` and we call this function **twice**. However, sometimes we may want to define a function but have it _called back_ at a later point in time. Consider another example:

```js
function printMessage(name) {
console.log(`My name is ${name}`);
}

setTimeout(printMessage, 3000, "Sally"); // <-- Call printMessage after at least 3000ms
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
setTimeout(printMessage, 3000, "Sally"); // <-- Call printMessage after at least 3000ms
setTimeout(printMessage, 3000, "Sally"); // <-- Call printMessage after at least 3000ms, with the argument "Sally"

printName("Daniel");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
printName("Daniel");
printMessage("Daniel");

```

In this example, we define the function and call `printMessage` just once. However, we're also using a built-in function called `setTimeout`. `setTimeout` allows us to set a minimum amount of time after which a function will be called back.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In this example, we define the function and call `printMessage` just once. However, we're also using a built-in function called `setTimeout`. `setTimeout` allows us to set a minimum amount of time after which a function will be called back.
In this example, we define the function and call `printMessage` just once. We also call it ourselves once. We're also using a built-in function called `setTimeout`. `setTimeout` allows us to set a minimum amount of time after which a function will be called back.


```js
setTimeout(printMessage, 3000, "Sally");
```

Let's break this down this call to `setTimeout`. It is saying:

> "After at least 3000 ms, call the function `printMessage`, and when you call back `printMessage`, pass the input of `"Sally"` to `printMessage`."

Notice we're saying _at least_ 3000 ms because `setTimeout` guarantees a minimum amount of time: it doesn't say that `printMessage` muse be called _exactly_ after 3000 ms. In this example, we say that `printMessage` is a {{<tooltip title="callback function">}}A callback function is a function that is passed as an argument to another function and gets executed after the main function has finished its execution.{{</tooltip>}} as it is _called back_ after 3000 miliseconds. In the terminal, we'll see "Daniel" appear first and then after an at least 3000 ms delay, we'll see the console log of "Sally"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Notice we're saying _at least_ 3000 ms because `setTimeout` guarantees a minimum amount of time: it doesn't say that `printMessage` muse be called _exactly_ after 3000 ms. In this example, we say that `printMessage` is a {{<tooltip title="callback function">}}A callback function is a function that is passed as an argument to another function and gets executed after the main function has finished its execution.{{</tooltip>}} as it is _called back_ after 3000 miliseconds. In the terminal, we'll see "Daniel" appear first and then after an at least 3000 ms delay, we'll see the console log of "Sally"
Notice we're saying _at least_ 3000 ms because `setTimeout` guarantees a minimum amount of time: it doesn't say that `printMessage` muse be called _exactly_ after 3000 ms. In this example, we say that `printMessage` is a {{<tooltip title="callback function">}}A callback function is a function that is passed as an argument to another function. We ourselves don't call the callback function - something else will call it for us at the right time.{{</tooltip>}} as it is _called back_ after 3000 miliseconds. In the terminal, we'll see "Daniel" appear first and then after an at least 3000 ms delay, we'll see the console log of "Sally".

A callback function is more general than this... In the context of setTimeout this tooltip seems correct, but more generally "some other code" will call a callback "when it should", right? Like [1, 2, 3].map(x => x + 1) - the arrow function there is still a callback function, but it will be executed before the main function finishes. And [].map(x => x + 1) - the callback function is never actually called. But these are still callback functions.

3 changes: 3 additions & 0 deletions org-cyf-itp/content/data-groups/sprints/3/prep/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ name="Calculating the remaining characters"
src="module/js2/update"
name="Updating the interface"
[[blocks]]
src="module/js2/timing"
name="Timers"
[[blocks]]
src="module/js2/events"
name="DOM events"
[[blocks]]
Expand Down
Loading