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

1-js/06-advanced-functions/08-settimeout-setinterval #118

Merged
merged 13 commits into from Jun 18, 2018
@@ -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 recursive `setTimeout`:
使用递归 `setTimeout`


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

// usage:
// 用例:
printNumbers(5, 10);
```

Note that in both solutions, there is an initial delay before the first output. Sometimes we need to add a line to make the first output immediately, that's easy to do.
这两种解法在首次输出时都有一个初始的延时,需要的话可以加一行让其立即输出,这并不难。

Expand Up @@ -2,12 +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 recursive `setTimeout`.
使用以下两种方法来实现。

1. 使用 `setInterval`。
2. 使用递归 `setTimeout`。
Expand Up @@ -2,11 +2,11 @@ importance: 4

---

# Rewrite setTimeout with setInterval
# 用 setInterval 重写 setTimeout

Here's the function that uses nested `setTimeout` to split a job into pieces.
下面的函数用嵌套 `setTimeout` 将任务分成多个部分。

Rewrite it to `setInterval`:
试试用 `setInterval` 进行重写:

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

// a piece of heavy job
// 任务的一部分
for(let j = 0; j < 1000000; j++) {
i++;
}
Expand Down
@@ -1,15 +1,16 @@

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