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 #182

Merged
merged 7 commits into from
Apr 2, 2023
Merged
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`:
Folosind `setInterval`:

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

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

Using nested `setTimeout`:
Folosind `setTimeout` imbricat:


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

// usage:
// utilizare:
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.
Observați că în ambele soluții există o întârziere inițială înainte de prima emitere. Funcția este apelată după `1000ms` pentru prima dată.

If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
Dacă dorim de asemenea ca funcția să ruleze imediat, atunci putem adăuga un apel suplimentar pe o linie separată, astfel:

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

---

# Output every second
# Emite în fiecare secundă

Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`.
Scrieți o funcție `printNumbers(from, to)` care emite un număr la fiecare secundă, începând de la `from` și terminând cu `to`.

Make two variants of the solution.
Creați două variante ale soluției.

1. Using `setInterval`.
2. Using nested `setTimeout`.
1. Folosind `setInterval`.
2. Folosind `setTimeout` imbricat.
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.
Orice `setTimeout` va rula numai după ce codul curent s-a terminat.

The `i` will be the last one: `100000000`.
`i` va fi ultimul: `100000000`.

```js run
let i = 0;

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

// assume that the time to execute this function is >100ms
// să presupunem că timpul de execuție a acestei funcții este >100ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Original file line number Diff line number Diff line change
@@ -2,25 +2,25 @@ importance: 5

---

# What will setTimeout show?
# Ce va arăta setTimeout?

In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
În codul de mai jos este planificat un apel `setTimeout`, apoi se rulează un calcul intensiv, care durează mai mult de 100ms pentru a se termina.

When will the scheduled function run?
Când se va executa funcția planificată?

1. After the loop.
2. Before the loop.
3. In the beginning of the loop.
1. După buclă.
2. Înainte de buclă.
3. La începutul buclei.


What is `alert` going to show?
Ce va arăta `alert`?

```js
let i = 0;

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

// assume that the time to execute this function is >100ms
// să presupunem că timpul de execuție a acestei funcții este >100ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Loading