Skip to content
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`:
Ved brug af `setInterval`:

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

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

Using nested `setTimeout`:
Ved brug af indlejret `setTimeout`:


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

// usage:
// brug:
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.
Bemærk at begge løsninger har en indledende forsinkelse før det første output. Funktionen kaldes efter `1000ms` den første gang.

If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
Hvis vi vil have funktionen til at køre med det samme, så kan vi tilføje et ekstra kald på en separat linje, som dette:

```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
# Output hvert sekund

Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`.
Skriv en funktion `printNumbers(from, to)` som udskriver et tal hvert sekund, startende fra `from` og afslutning med `to`.

Make two variants of the solution.
Lav to varianter af løsningen.

1. Using `setInterval`.
2. Using nested `setTimeout`.
1. Brug `setInterval`.
2. Brug indlejret `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.
En `setTimeout` vil kun køre efter at det nuværende kode er færdig.

The `i` will be the last one: `100000000`.
Variablen `i` vil derfor have værdien `100000000`.

```js run
let i = 0;

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

// assume that the time to execute this function is >100ms
// Forestil dig at tiden for at køre dette er mere end 100ms
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?
# Hvad vil setTimeout vise?

In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
I koden nedenfor bliver et `setTimeout` kald planlagt. Derefter køres en tung beregning, som tager mere end 100ms at fuldføre.

When will the scheduled function run?
Hvornår vil det planlagte kald køres?

1. After the loop.
2. Before the loop.
3. In the beginning of the loop.
1. Efter løkken.
2. Før løkken.
3. I begyndelsen af løkken.


What is `alert` going to show?
Hvad vil `alert` vise?

```js
let i = 0;

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

// assume that the time to execute this function is >100ms
// Forestil dig at tiden for at køre dette er mere end 100ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Loading