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
Copy file name to clipboardExpand all lines: 1-js/11-async/01-callbacks/article.md
+6-3Lines changed: 6 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,8 @@ If there's any code below `loadScript(…)`, it doesn't wait until the script lo
43
43
44
44
```js
45
45
loadScript('/my/script.js');
46
-
// the code below loadScript doesn't wait for the script loading to finish
46
+
// the code below loadScript
47
+
// doesn't wait for the script loading to finish
47
48
// ...
48
49
```
49
50
@@ -59,7 +60,7 @@ newFunction(); // no such function!
59
60
*/!*
60
61
```
61
62
62
-
Naturally, the browser probably didn't have time to load the script. So the immediate call to the new function fails. As of now, the `loadScript` function doesn't provide a way to track the load completion. The script loads and eventually runs, that's all. But we'd like to know when it happens, to use new functions and variables from that script.
63
+
Naturally, the browser probably didn't have time to load the script. As of now, the `loadScript` function doesn't provide a way to track the load completion. The script loads and eventually runs, that's all. But we'd like to know when it happens, to use new functions and variables from that script.
63
64
64
65
Let's add a `callback` function as a second argument to `loadScript` that should execute when the script loads:
65
66
@@ -76,6 +77,8 @@ function loadScript(src, *!*callback*/!*) {
76
77
}
77
78
```
78
79
80
+
The `onload` event is described in the article <info:onload-onerror#loading-a-script>, it basically executes a function after the script is loaded and executed.
81
+
79
82
Now if we want to call new functions from the script, we should write that in the callback:
80
83
81
84
```js
@@ -101,7 +104,7 @@ function loadScript(src, callback) {
0 commit comments