Skip to content

Commit d7414ce

Browse files
committed
addded code frun to completion
1 parent 75e7109 commit d7414ce

File tree

7 files changed

+3192
-0
lines changed

7 files changed

+3192
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Run to completion
2+
3+
### 1. Let's have a look to the code that we have in `main.js`
4+
5+
```javascript
6+
btn.addEventListener('click', (evt) => {
7+
evt.stopPropagation();
8+
modifyPage();
9+
blockingOperation();
10+
});
11+
```
12+
* We have an event listener for the click of the button.
13+
14+
* In this listener we have three statements, first, we stop the propagation of the event, then we modify the page changing its background color and adding a p tag element, for last we have a `blocking operation`.
15+
16+
* Before click the button ask the students, what they think it's going to happen?
17+
* When we click the button, we can watck the screen block, after 10 seconds the screen turns lime, the text is added, and we can interact with the screen again.
18+
* When we get to the blocking process, `blockingOperation()` statement, the statement that it's going to be around 10 seconds, anything else can be executed, what means that we can interact with the browser.
19+
20+
* The event listener callback, it's executed till completion, that includes obviously the blocking process. If it takes 10 seconds to get completed, nothing else in that range of time will be executed.
21+
22+
### Challange Little programs. How many little programs are here?
23+
24+
```javascript
25+
document.onreadystatechange = () => {
26+
if(document.readyState === 'complete') {
27+
const btn = document.getElementById("button");
28+
btn.addEventListener('click', (evt) => {
29+
evt.stopPropagation();
30+
modifyPage();
31+
blockingOperation();
32+
});
33+
34+
const modifyPage = () => {
35+
// modify page
36+
document.body.style.backgroundColor = 'lime';
37+
let p = document.createElement("p");
38+
p.innerText = "let's add some text to the page";
39+
document.body.appendChild(p);
40+
};
41+
42+
const blockingOperation = () => {
43+
// simulate blocking / long running operation
44+
const start = Date.now();
45+
const delaySeconds = 10;
46+
while (Date.now() < start + delaySeconds * 1000) {}
47+
}
48+
}
49+
};
50+
```
51+
* 3 | 2
52+
* 3 if we consider, the part that the browser repaints the screen. We can think this way because it's not executed until the previous statement it's not completed.

gulpfile.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const gulp = require('gulp'),
4+
connect = require('gulp-connect'),
5+
open = require('gulp-open'),
6+
options = {
7+
port: 9005,
8+
root: ['src'],
9+
devBase: 'http://localhost:',
10+
browsers: ['safari', 'chrome']
11+
},
12+
openOptions = {
13+
uri: options.devBase + options.port,
14+
app: options.browser
15+
};
16+
17+
gulp.task('connect', () => connect.server({
18+
root: options.root,
19+
port: options.port
20+
}));
21+
// https://github.com/stevelacy/gulp-open/issues/15
22+
gulp.task('open', () => gulp.src(__filename).pipe(open(openOptions)));
23+
24+
gulp.task('default', ['connect', 'open']);

0 commit comments

Comments
 (0)