Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduling: setTimeout and setInterval #187

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Using `setInterval`:
Використовуючи `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`:
Використовуючи вкладений `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.
Зауважте, що в обох рішеннях є початкова затримка перед першим виходом. Функція викликається після `1000 мс` у перший раз.

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`.
Напишіть функцію `printNumbers(from, to)` яка виводить число кожну секунду, починаючи від `from` і закінчуючи `to`.

Make two variants of the solution.
Зробіть два варіанти рішення.

1. Using `setInterval`.
2. Using nested `setTimeout`.
1. Використовуючи `setInterval`.
2. Використовуючи вкладений `setTimeout`.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

Any `setTimeout` will run only after the current code has finished.
Будь-який `setTimeout` запускатиметься лише після завершення поточного коду.

The `i` will be the last one: `100000000`.
`i` буде останнім: `100000000`.

```js run
let i = 0;

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

// assume that the time to execute this function is >100ms
// припустимо, що час виконання цієї функції > 100 мс
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ importance: 5

---

# What will setTimeout show?
# Що покаже setTimeout?

In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
У наведеному нижче коді заплановано виклик `setTimeout`, потім виконується важкий розрахунок, на виконання якого потрібно більше 100 мс.

When will the scheduled function run?
Коли запуститься запланована функція?

1. After the loop.
2. Before the loop.
3. In the beginning of the loop.
1. Після циклу.
2. До циклу.
3. На початку циклу.


What is `alert` going to show?
Що буде показувати `alert`?

```js
let i = 0;

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

// assume that the time to execute this function is >100ms
// припустимо, що час виконання цієї функції > 100 мс
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Loading