◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Using `setInterval`:

```js run
function printNumbers(from, to) {
Expand All @@ -14,11 +14,11 @@ function printNumbers(from, to) {
}, 1000);
}

// usage:
printNumbers(5, 10);
```

Using nested `setTimeout`:


```js run
Expand All @@ -34,13 +34,13 @@ function printNumbers(from, to) {
}, 1000);
}

// usage:
printNumbers(5, 10);
```

Note that in both solutions, there is an initial delay before the first output. The function is called after `1000ms` the first time.

If we also want the function to run immediately, then we can add an additional call on a separate line, like this:

```js run
function printNumbers(from, to) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Output every second

Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`.

Make two variants of the solution.

1. Using `setInterval`.
2. Using nested `setTimeout`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ importance: 5

---

# What will setTimeout show?

In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.

When will the scheduled function run?

1. After the loop.
2. Before the loop.
3. In the beginning of the loop.


What is `alert` going to show?

```js
let i = 0;

setTimeout(() => alert(i), 100); // ?

// assume that the time to execute this function is >100ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Loading
Toggle all file notes Toggle all file annotations