Skip to content

Traduction de "Scheduling: setTimeout and setInterval" #33

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

Merged
merged 1 commit into from
Jul 3, 2019
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`:
Avec `setInterval`:

```js run
function printNumbers(from, to) {
Expand All @@ -18,7 +18,7 @@ function printNumbers(from, to) {
printNumbers(5, 10);
```

Using recursive `setTimeout`:
Avec `setTimeout` de façon récursive :


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

// usage:
// utilisation :
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.
Notons que, dans les deux solutions, il y a un délai initial avant le premier résultat. En effet, la fonction est appelée pour la première fois au bout de `1000ms`.

If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
Afin d'exécuter la fonction immédiatement, on peut ajouter un autre appel avant `setInterval`.

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

---

# Output every second
# Un résultat par seconde

Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`.
Écrire une fonction `printNumbers(from, to)` qui affiche un nombre par seconde, en partant de `from` jusqu'à `to`.

Make two variants of the solution.

1. Using `setInterval`.
2. Using recursive `setTimeout`.
Implémenter deux variantes de la solution en :

1. utilisant `setInterval`,
2. utilisant `setTimeout` de façon récursive.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 4

---

# Rewrite setTimeout with setInterval
# Réécrire setTimeout avec setInterval

Here's the function that uses nested `setTimeout` to split a job into pieces.
Voici une fonction qui utilise un `setTimeout` imbriqué pour découper une tâche en petit bouts.

Rewrite it to `setInterval`:
Réécrire le bloc suivant en utilisant `setInterval`:

```js run
let i = 0;
Expand All @@ -21,7 +21,7 @@ function count() {
setTimeout(count);
}

// a piece of heavy job
// un morceau d'une très grosse tâche
for(let j = 0; j < 1000000; j++) {
i++;
}
Expand Down
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` ne peut s'exécuter qu'une fois le bloc de code courant terminé.

The `i` will be the last one: `100000000`.
Le `i` sera donc le dernier : `100000000`.

```js run
let i = 0;

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

// assume that the time to execute this function is >100ms
// on considère que cette fonction met plus de 100ms à s'exécuter
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,24 @@ importance: 5

---

# What will setTimeout show?
# Que va afficher setTimeout ?

In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
Dans le code ci-dessous il y a une exécution planifié par `setTimeout`, suivie par un calcul conséquent qui prend plus de 100ms à tourner.

When will the scheduled function run?
Quand la fonction planifiée va-t-elle s'exécuter ?

1. After the loop.
2. Before the loop.
3. In the beginning of the loop.
1. Après la boucle.
2. Avant la boucle.
3. Au début de la boucle.


What is `alert` going to show?
Qu'est-ce que `alert` va afficher ?

```js
let i = 0;

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

// assume that the time to execute this function is >100ms
// on considère que cette fonction met plus de 100ms à s'exécuter
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Loading