You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// e refers to the click event, and e.target to the elment clicked
16
-
console.log(e.target.id);
15
+
// e refers to the click event, and e.target to the elment clicked
16
+
console.log(e.target.id);
17
17
}
18
18
19
19
btn1.addEventListener("click", listenForClick);
@@ -30,19 +30,19 @@ function listenForClick (e) {
30
30
console.log(e.target.id);
31
31
// Check which button was clicked, and track it
32
32
if(e.target.id==='btn1')
33
-
btn1Clicked =true;
33
+
btn1Clicked =true;
34
34
else
35
-
btn2Clicked =true;
36
-
35
+
btn2Clicked =true;
36
+
37
37
// reset both to false after a second
38
-
setTimeout(() => {
39
-
btn1Clicked =false;
38
+
setTimeout(() => {
39
+
btn1Clicked =false;
40
40
btn2Clicked =false;
41
41
}, 1000)
42
42
43
43
// If both have been tracked as true in the past 1 sec
44
44
if(btn1Clicked && btn2Clicked)
45
-
console.log("--success--");
45
+
console.log("--success--");
46
46
}
47
47
```
48
48
@@ -83,11 +83,11 @@ const id = setInterval(() => {
83
83
That works well. But what if we want to apply this sort of animation again, for different colors? It would make sense to turn it into a function which we can call again:
@@ -96,14 +96,14 @@ function applyForEach(color, delay){
96
96
}, delay);
97
97
}
98
98
99
-
//applyForEach("red", 500);
100
-
applyForEach("blue", 300);
99
+
//applyBackgroundForEach("red", 500);
100
+
applyBackgroundForEach("blue", 300);
101
101
```
102
102
103
103
Our function now lets us switch between which color we want to apply for all the list items. However, it is not flexible enough to let us do other operations on the list items. What if we want to change the `text-decoration` or `font-size`? Or even delete the list items? What if we could run an arbitary operation i.e. `function` on all of the list items:
In the above, `applyForEach` now takes an argument `action` which is a `function` that is applied to each of the list items. `action` is therefore a **callback function**. As you can see, callback functions can be both *synchronous* like the one here, or *asynchronous* as the one discussed in class.
0 commit comments