Skip to content

Commit 9c3ac13

Browse files
committed
draft
1 parent 65671ab commit 9c3ac13

File tree

23 files changed

+316
-259
lines changed

23 files changed

+316
-259
lines changed

Diff for: 1-js/02-first-steps/03-strict-mode/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Even if we press `key:Shift+Enter` to input multiple lines, and put `use strict`
5757
5858
The reliable way to ensure `use strict` would be to input the code into console like this:
5959
60-
```
60+
```js
6161
(function() {
6262
'use strict';
6363

Diff for: 1-js/06-advanced-functions/08-settimeout-setinterval/3-rewrite-settimeout/solution.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let i = 0;
55

66
let start = Date.now();
77

8-
let timer = setInterval(count, 0);
8+
let timer = setInterval(count);
99

1010
function count() {
1111

@@ -20,4 +20,3 @@ function count() {
2020

2121
}
2222
```
23-

Diff for: 1-js/06-advanced-functions/08-settimeout-setinterval/3-rewrite-settimeout/task.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function count() {
1818
if (i == 1000000000) {
1919
alert("Done in " + (Date.now() - start) + 'ms');
2020
} else {
21-
setTimeout(count, 0);
21+
setTimeout(count);
2222
}
2323

2424
// a piece of heavy job

Diff for: 1-js/06-advanced-functions/08-settimeout-setinterval/article.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ These methods are not a part of JavaScript specification. But most environments
1515
The syntax:
1616

1717
```js
18-
let timerId = setTimeout(func|code, delay[, arg1, arg2...])
18+
let timerId = setTimeout(func|code, [delay], [arg1], [arg2], ...)
1919
```
2020

2121
Parameters:
@@ -25,7 +25,7 @@ Parameters:
2525
Usually, that's a function. For historical reasons, a string of code can be passed, but that's not recommended.
2626

2727
`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.
2929

3030
`arg1`, `arg2`...
3131
: 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
110110
The `setInterval` method has the same syntax as `setTimeout`:
111111

112112
```js
113-
let timerId = setInterval(func|code, delay[, arg1, arg2...])
113+
let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...)
114114
```
115115

116116
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,
238238

239239
## setTimeout(...,0)
240240

241-
There's a special use case: `setTimeout(func, 0)`.
241+
There's a special use case: `setTimeout(func, 0)`, or just `setTimeout(func)`.
242242

243243
This schedules the execution of `func` as soon as possible. But scheduler will invoke it only after the current code is complete.
244244

@@ -247,7 +247,7 @@ So the function is scheduled to run "right after" the current code. In other wor
247247
For instance, this outputs "Hello", then immediately "World":
248248

249249
```js run
250-
setTimeout(() => alert("World"), 0);
250+
setTimeout(() => alert("World"));
251251

252252
alert("Hello");
253253
```
@@ -260,7 +260,7 @@ There's a trick to split CPU-hungry tasks using `setTimeout`.
260260

261261
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.
262262

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.
264264

265265
For clarity, let's take a simpler example for consideration. We have a function to count from `1` to `1000000000`.
266266

@@ -303,7 +303,7 @@ function count() {
303303
if (i == 1e9) {
304304
alert("Done in " + (Date.now() - start) + 'ms');
305305
} else {
306-
setTimeout(count, 0); // schedule the new call (**)
306+
setTimeout(count); // schedule the new call (**)
307307
}
308308

309309
}
@@ -338,7 +338,7 @@ function count() {
338338

339339
// move the scheduling at the beginning
340340
if (i < 1e9 - 1e6) {
341-
setTimeout(count, 0); // schedule the new call
341+
setTimeout(count); // schedule the new call
342342
}
343343

344344
do {
@@ -371,8 +371,8 @@ setTimeout(function run() {
371371
times.push(Date.now() - start); // remember delay from the previous call
372372
373373
if (start + 100 < Date.now()) alert(times); // show the delays after 100ms
374-
else setTimeout(run, 0); // else re-schedule
375-
}, 0);
374+
else setTimeout(run); // else re-schedule
375+
});
376376
377377
// an example of the output:
378378
// 1,1,1,1,9,15,20,24,30,35,40,45,50,55,59,64,70,75,80,85,90,95,100
@@ -430,7 +430,7 @@ And if we use `setTimeout` to split it into pieces then changes are applied in-b
430430
} while (i % 1e3 != 0);
431431
432432
if (i < 1e9) {
433-
setTimeout(count, 0);
433+
setTimeout(count);
434434
}
435435
436436
}

Diff for: 8-web-components/1-webcomponents-intro/article.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ So, what comprises a component?
6767

6868
- [Custom elements](https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements) -- to define custom HTML elements.
6969
- [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.
7171

7272
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.
7373

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Please note:
3+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<script src="time-formatted.js"></script>
3+
<script src="live-timer.js"></script>
4+
5+
<live-timer id="elem"></live-timer>
6+
7+
<script>
8+
elem.addEventListener('tick', event => console.log(event.detail));
9+
</script>

Diff for: 8-web-components/2-custom-elements/1-custom-timer/time-formatted.js renamed to 8-web-components/2-custom-elements/1-live-timer/solution.view/time-formatted.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ class TimeFormatted extends HTMLElement {
1414
}).format(date);
1515
}
1616

17-
connectedCallback() { // (2)
17+
connectedCallback() {
1818
if (!this.rendered) {
1919
this.render();
2020
this.rendered = true;
2121
}
2222
}
2323

24-
static get observedAttributes() { // (3)
24+
static get observedAttributes() {
2525
return ['datetime', 'year', 'month', 'day', 'hour', 'minute', 'second', 'time-zone-name'];
2626
}
2727

28-
attributeChangedCallback(name, oldValue, newValue) { // (4)
28+
attributeChangedCallback(name, oldValue, newValue) {
2929
this.render();
3030
}
3131

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<!-- don't modify this -->
3+
<script src="time-formatted.js"></script>
4+
5+
<!-- your code here: -->
6+
<script src="live-timer.js"></script>
7+
8+
<live-timer id="elem"></live-timer>
9+
10+
<script>
11+
elem.addEventListener('tick', event => console.log(event.detail));
12+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class LiveTimer extends HTMLElement {
2+
3+
/* your code here */
4+
5+
}
6+
7+
customElements.define("live-timer", LiveTimer);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class TimeFormatted extends HTMLElement {
2+
3+
render() {
4+
let date = new Date(this.getAttribute('datetime') || Date.now());
5+
6+
this.innerHTML = new Intl.DateTimeFormat("default", {
7+
year: this.getAttribute('year') || undefined,
8+
month: this.getAttribute('month') || undefined,
9+
day: this.getAttribute('day') || undefined,
10+
hour: this.getAttribute('hour') || undefined,
11+
minute: this.getAttribute('minute') || undefined,
12+
second: this.getAttribute('second') || undefined,
13+
timeZoneName: this.getAttribute('time-zone-name') || undefined,
14+
}).format(date);
15+
}
16+
17+
connectedCallback() {
18+
if (!this.rendered) {
19+
this.render();
20+
this.rendered = true;
21+
}
22+
}
23+
24+
static get observedAttributes() {
25+
return ['datetime', 'year', 'month', 'day', 'hour', 'minute', 'second', 'time-zone-name'];
26+
}
27+
28+
attributeChangedCallback(name, oldValue, newValue) {
29+
this.render();
30+
}
31+
32+
}
33+
34+
customElements.define("time-formatted", TimeFormatted);
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# Live timer element
3+
4+
We already have `<time-formatted>` element to show a nicely formatted time.
5+
6+
Create `<live-timer>` element to show the current time:
7+
1. It should use `<time-formatted>` internally, not duplicate its functionality.
8+
2. Ticks (updates) every second.
9+
3. For every tick, a custom event named `tick` should be generated, with the current date in `event.detail` (see chapter <info:dispatch-events>).
10+
11+
Usage:
12+
13+
```html
14+
<live-timer id="elem"></live-timer>
15+
16+
<script>
17+
elem.addEventListener('tick', event => console.log(event.detail));
18+
</script>
19+
```
20+
21+
Demo:
22+
23+
[iframe src="solution" height=40]

0 commit comments

Comments
 (0)