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/06-advanced-functions/08-settimeout-setinterval/article.md
+11-11
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ These methods are not a part of JavaScript specification. But most environments
15
15
The syntax:
16
16
17
17
```js
18
-
let timerId =setTimeout(func|code, delay[, arg1, arg2...])
18
+
let timerId =setTimeout(func|code, [delay], [arg1], [arg2], ...)
19
19
```
20
20
21
21
Parameters:
@@ -25,7 +25,7 @@ Parameters:
25
25
Usually, that's a function. For historical reasons, a string of code can be passed, but that's not recommended.
26
26
27
27
`delay`
28
-
: The delay before run, in milliseconds (1000 ms = 1 second).
28
+
: The delay before run, in milliseconds (1000 ms = 1 second), by default 0.
29
29
30
30
`arg1`, `arg2`...
31
31
: Arguments for the function (not supported in IE9-)
@@ -110,7 +110,7 @@ For browsers, timers are described in the [timers section](https://www.w3.org/TR
110
110
The `setInterval` method has the same syntax as `setTimeout`:
111
111
112
112
```js
113
-
let timerId =setInterval(func|code, delay[, arg1, arg2...])
113
+
let timerId =setInterval(func|code, [delay], [arg1], [arg2], ...)
114
114
```
115
115
116
116
All arguments have the same meaning. But unlike `setTimeout` it runs the function not only once, but regularly after the given interval of time.
@@ -238,7 +238,7 @@ There's a side-effect. A function references the outer lexical environment, so,
238
238
239
239
## setTimeout(...,0)
240
240
241
-
There's a special use case: `setTimeout(func, 0)`.
241
+
There's a special use case: `setTimeout(func, 0)`, or just `setTimeout(func)`.
242
242
243
243
This schedules the execution of `func` as soon as possible. But scheduler will invoke it only after the current code is complete.
244
244
@@ -247,7 +247,7 @@ So the function is scheduled to run "right after" the current code. In other wor
247
247
For instance, this outputs "Hello", then immediately "World":
248
248
249
249
```js run
250
-
setTimeout(() =>alert("World"), 0);
250
+
setTimeout(() =>alert("World"));
251
251
252
252
alert("Hello");
253
253
```
@@ -260,7 +260,7 @@ There's a trick to split CPU-hungry tasks using `setTimeout`.
260
260
261
261
For instance, a syntax-highlighting script (used to colorize code examples on this page) is quite CPU-heavy. To highlight the code, it performs the analysis, creates many colored elements, adds them to the document -- for a big text that takes a lot. It may even cause the browser to "hang", which is unacceptable.
262
262
263
-
So we can split the long text into pieces. First 100 lines, then plan another 100 lines using `setTimeout(...,0)`, and so on.
263
+
So we can split the long text into pieces. First 100 lines, then plan another 100 lines using `setTimeout(...,0)`, and so on.
264
264
265
265
For clarity, let's take a simpler example for consideration. We have a function to count from `1` to `1000000000`.
266
266
@@ -303,7 +303,7 @@ function count() {
303
303
if (i ==1e9) {
304
304
alert("Done in "+ (Date.now() - start) +'ms');
305
305
} else {
306
-
setTimeout(count, 0); // schedule the new call (**)
306
+
setTimeout(count); // schedule the new call (**)
307
307
}
308
308
309
309
}
@@ -338,7 +338,7 @@ function count() {
338
338
339
339
// move the scheduling at the beginning
340
340
if (i <1e9-1e6) {
341
-
setTimeout(count, 0); // schedule the new call
341
+
setTimeout(count); // schedule the new call
342
342
}
343
343
344
344
do {
@@ -371,8 +371,8 @@ setTimeout(function run() {
371
371
times.push(Date.now() - start); // remember delay from the previous call
372
372
373
373
if (start + 100 < Date.now()) alert(times); // show the delays after 100ms
Copy file name to clipboardExpand all lines: 8-web-components/1-webcomponents-intro/article.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ So, what comprises a component?
67
67
68
68
-[Custom elements](https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements) -- to define custom HTML elements.
69
69
-[Shadow DOM](https://dom.spec.whatwg.org/#shadow-trees) -- to create an internal DOM for the component, hidden from the others.
70
-
-[CSS Scoping](https://drafts.csswg.org/css-scoping/) -- to declare styles that only apply inside the component.
70
+
-[CSS Scoping](https://drafts.csswg.org/css-scoping/) -- to declare styles that only apply inside the Shadow DOM of the component.
71
71
72
72
There exist many frameworks and development methodologies that aim to do the similar thing, each one with its own bells and whistles. Usually, special CSS classes and conventions are used to provide "component feel" -- CSS scoping and DOM encapsulation.
1. We clear `setInterval` timer when the element is removed from the document. That's important, otherwise it continues ticking even if not needed any more. And the browser can't clear the memory from this element and referenced by it.
4
+
2. We can access current date as `elem.date` property. All class methods and properties are naturally element methods and properties.
0 commit comments