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
Merged

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

merged 13 commits into from Jun 18, 2018

Conversation

pot-code
Copy link
Contributor

@pot-code pot-code commented Jun 8, 2018

翻译完成,resolve #88

改几个位置的表达方式
@sunhaokk
Copy link
Contributor

校对认领

Copy link
Contributor

@sunhaokk sunhaokk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

翻译不错,建议

@@ -1,13 +1,12 @@
importance: 5
重要性:5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

老大说 importance 不用翻译

@@ -1,12 +1,12 @@
importance: 4
重要性:4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@@ -1,36 +1,36 @@
# Scheduling: setTimeout and setInterval
# 调度:setTimeout setInterval
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议上下文 Scheduling 翻译一致

@@ -199,72 +199,72 @@ setTimeout(function run() {
}, 100);
```

For `setInterval` the internal scheduler will run `func(i)` every 100ms:
`setInterval` 而言,内部的调度器会每间隔100 毫秒执行一次 `func(i)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

数字 空格

- To cancel the execution, we should call `clearInterval/clearTimeout` with the value returned by `setInterval/setTimeout`.
- Nested `setTimeout` calls is a more flexible alternative to `setInterval`. Also they can guarantee the minimal time *between* the executions.
- Zero-timeout scheduling `setTimeout(...,0)` is used to schedule the call "as soon as possible, but after the current code is complete".
- `setInterval(func, delay, ...args)` 和 `setTimeout(func, delay, ...args)` 可以让 `func` 在经历一段延时后周期性/一次性执行。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定期/延迟毫秒后一次性执行

- The CPU is overloaded.
- The browser tab is in the background mode.
- The laptop is on battery.
浏览器内部的定时器会因各种原因而出现延迟,譬如:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了避免歧义,建议延迟,改成降速

@athena0304
Copy link
Contributor

校对认领 @leviding

@leviding
Copy link
Member

@sunhaokk @athena0304 ok


```js run no-beautify
setTimeout("alert('Hello')", 1000);
```

But using strings is not recommended, use functions instead of them, like this:
但是,毕竟这种方式并不推崇,所以建议还是用函数格式:

```js run no-beautify
setTimeout(() => alert('Hello'), 1000);
```

````smart header="Pass a function, but don't run it"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Pass a function, but don't run it] 标题未翻译

```
That doesn't work, because `setTimeout` expects a reference to function. And here `sayHi()` runs the function, and the *result of its execution* is passed to `setTimeout`. In our case the result of `sayHi()` is `undefined` (the function returns nothing), so nothing is scheduled.
```
为什么不行呢,因为 `setTimeout` 需要的是函数的引用。而这里的 `sayHi()` 很明显是在执行函数,所以实际上传入 `setTimeout` 的是 *函数的执行结果*。在这个例子中,`sayHi()` 的执行结果是 `undefined`(也就是说函数没有返回任何结果),所以实际上什么也没有调度。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

函数的执行结果】这里需要加粗


A call to `setTimeout` returns a "timer identifier" `timerId` that we can use to cancel the execution.
`setTimeout` 在调用时会返回一个“定时器 id”,接下来使用变量 `timerId` 来取消调度。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setTimeout 在调用时会返回一个“定时器 id”,接下来使用变量 timerId 来取消调度。=> setTimeout 在调用时会返回一个“定时器 id”timerId,接下来可以用它来取消调度。


```js
let timerId = setTimeout(...);
clearTimeout(timerId);
```

In the code below, we schedule the function and then cancel it (changed our mind). As a result, nothing happens:
在上面的代码中,我们对一个函数进行了调度,紧接着取消了这次调度(中途反悔了)。所以最后啥也没发生:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[上面] => [下面]

let timerId = setInterval(() => alert('tick'), 2000);

// after 5 seconds stop
// 5 秒之后停止
setTimeout(() => { clearInterval(timerId); alert('stop'); }, 5000);
```

```smart header="Modal windows freeze time in Chrome/Opera/Safari"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

标题未翻译


Let's demonstrate what it means with the example below. The `setTimeout` call in it re-schedules itself after `0ms`. Each call remembers the real time from the previous one in the `times` array. What do the real delays look like? Let's see:
下面用具体示例来阐述。其中 `setTimeout` 每次都在 `0ms` 后就再安排一次递归,每次调用都会在 `times` 数组中记录下上一次调用的实际时间。所以,最终延时如何?下面来揭晓:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

【记录下上一次调用】=> 【记录上一次调用】


Another benefit for in-browser scripts is that they can show a progress bar or something to the user. That's because the browser usually does all "repainting" after the script is complete.
行间脚本还有个益处,可以用来向用户展示进度条等。因为浏览器在脚本执行完后,就会开始所有的 “repainting” 过程。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

【因为浏览器在脚本执行完后,就会开始所有的 “repainting” 过程。】=> 【因为浏览器通常是在所有的脚本加载完毕后再做全部的“重绘”工作】


So if we do a single huge function then even if it changes something, the changes are not reflected in the document till it finishes.
所以,如果运行一个非常耗时的函数,即便在这个函数中改变了文档内容,除非这个函数执行完,那么变化是不会立刻反映到页面上的。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

【那么变化是不会立刻反映到页面上的】=> 【否则变化是不会立刻反映到页面上的】

- To split CPU-hungry tasks into pieces, so that the script doesn't "hang"
- To let the browser do something else while the process is going on (paint the progress bar).
`setTimeout(...,0)` 的一些用法示例:
- 将耗费的 CPU 任务分割成多块,这样脚本运行不会进入“挂起”状态。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

【将耗费的 CPU 任务分割成多块】=> 【将耗费 CPU 的任务分割成多块】

- To let the browser do something else while the process is going on (paint the progress bar).
`setTimeout(...,0)` 的一些用法示例:
- 将耗费的 CPU 任务分割成多块,这样脚本运行不会进入“挂起”状态。
- 进程繁忙时也能让浏览器抽身做其他事(例如绘制进度条)。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

【做其他事】=> 【做其它事情】

@athena0304
Copy link
Contributor

@pot-code @leviding 校对完成

@leviding
Copy link
Member

@pot-code 可以修改啦

根据校对意见修改
@pot-code
Copy link
Contributor Author

@sunhaokk @athena0304 感谢二位的细心校对,@athena0304 真的很严格,哈哈哈哈!十分感谢!挑出了很多不容易发现的问题。

@leviding 修改完毕

@leviding leviding added the WIP Work in process label Jun 13, 2018
@leviding
Copy link
Member

很赞,整体很符合规范。只是有几个细节问题我已修改,按下最新的 commit 记录,下次注意下。

@leviding leviding merged commit 2a58a4e into javascript-tutorial:zh-hans Jun 18, 2018
@leviding leviding removed WIP Work in process 正在校对 labels Jun 18, 2018
@pot-code
Copy link
Contributor Author

@leviding 辛苦了!task 的问题校对提到了,结果忘记了这个事情,实在是大误。毕竟翻译老鸟了,规范已经熟悉了😏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants