Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Scheduling: setTimeout and setInterval#170

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
otmon76 merged 1 commit intojavascript-tutorial:masterfromotmon76:1.6.8
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@

Using `setInterval`:
Pomocí `setInterval`:

```js run
functionprintNumbers(from, to) {
letcurrent =from;
functionvypišČísla(začátek, konec) {
letaktuální =začátek;

lettimerId = setInterval(function() {
alert(current);
if (current ==to) {
clearInterval(timerId);
letidČasovače = setInterval(function() {
alert(aktuální);
if (aktuální ==konec) {
clearInterval(idČasovače);
}
current++;
aktuální++;
}, 1000);
}

//usage:
printNumbers(5, 10);
//použití:
vypišČísla(5, 10);
```

Using nested `setTimeout`:
Pomocí vnořeného `setTimeout`:


```js run
functionprintNumbers(from, to) {
letcurrent =from;
functionvypišČísla(začátek, konec) {
letaktuální =začátek;

setTimeout(functiongo() {
alert(current);
if (current <to) {
setTimeout(go, 1000);
setTimeout(functionspusť() {
alert(aktuální);
if (aktuální <konec) {
setTimeout(spusť, 1000);
}
current++;
aktuální++;
}, 1000);
}

//usage:
printNumbers(5, 10);
//použití:
vypišČísla(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.
Všimněte si, že v obou řešeních je úvodní prodleva před prvním výstupem. Funkce je poprvé volána za `1000 ms`.

If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
Jestliže chceme, aby se funkce spustila okamžitě, můžeme přidat další volání na samostatný řádek, například takto:

```js run
functionprintNumbers(from, to) {
letcurrent =from;
functionvypišČísla(začátek, konec) {
letaktuální =začátek;

functiongo() {
alert(current);
if (current ==to) {
clearInterval(timerId);
functionspusť() {
alert(aktuální);
if (aktuální ==konec) {
clearInterval(idČasovače);
}
current++;
aktuální++;
}

*!*
go();
spusť();
*/!*
lettimerId = setInterval(go, 1000);
letidČasovače = setInterval(spusť, 1000);
}

printNumbers(5, 10);
vypišČísla(5, 10);
```
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,11 @@ importance: 5

---

#Output every second
#Výstup každou sekundu

Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`.
Napište funkci `vypišČísla(začátek, konec)`, která každou sekundu vypíše číslo, přičemž začne číslem `začátek` a skončí číslem `konec`.

Make two variants of the solution.
Vytvořte dvě varianty řešení.

1.Using `setInterval`.
2.Using nested `setTimeout`.
1.Pomocí `setInterval`.
2.Pomocí vnořeného `setTimeout`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@

Any `setTimeout`will run only after the current code has finished.
Každý `setTimeout`se spustí teprve po dokončení aktuálního kódu.

The `i`will be the last one: `100000000`.
Proměnná `i`bude obsahovat poslední hodnotu: `100000000`.

```js run
let i = 0;

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

//assume that the time to execute this function is >100ms
//předpokládáme, že doba běhu této funkce je větší než 100 ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,25 +2,25 @@ importance: 5

---

#What will setTimeout show?
#Co zobrazí setTimeout?

In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
V následujícím kódu je načasováno volání `setTimeout`, pak proběhne náročný výpočet, jehož dokončení bude trvat více než 100 ms.

When will the scheduled function run?
Kdy se načasovaná funkce spustí?

1.After the loop.
2.Before the loop.
3.In the beginning of the loop.
1.Po cyklu.
2.Před cyklem.
3.Na začátku cyklu.


What is `alert` going to show?
Co zobrazí `alert`?

```js
let i = 0;

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

//assume that the time to execute this function is >100ms
//předpokládáme, že doba výkonu této funkce je větší než 100 ms
for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp