Skip to content

Commit

Permalink
zh-cn: Format /web/api using Prettier (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
queengooborg committed Jul 28, 2023
1 parent 09c0ef4 commit 6add031
Show file tree
Hide file tree
Showing 100 changed files with 1,979 additions and 1,792 deletions.
14 changes: 7 additions & 7 deletions files/zh-cn/web/api/abortcontroller/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ slug: Web/API/AbortController

```js
let controller;
const url = 'video.mp4';
const url = "video.mp4";

const downloadBtn = document.querySelector('.download');
const abortBtn = document.querySelector('.abort');
const downloadBtn = document.querySelector(".download");
const abortBtn = document.querySelector(".abort");

downloadBtn.addEventListener('click', fetchVideo);
downloadBtn.addEventListener("click", fetchVideo);

abortBtn.addEventListener('click', () => {
abortBtn.addEventListener("click", () => {
if (controller) {
controller.abort();
console.log('中止下载');
console.log("中止下载");
}
});

Expand All @@ -55,7 +55,7 @@ function fetchVideo() {
const signal = controller.signal;
fetch(url, { signal })
.then((response) => {
console.log('下载完成', response);
console.log("下载完成", response);
})
.catch((err) => {
console.error(`下载错误:${err.message}`);
Expand Down
14 changes: 7 additions & 7 deletions files/zh-cn/web/api/abortcontroller/signal/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ slug: Web/API/AbortController/signal
const controller = new AbortController();
const signal = controller.signal;

const url = 'video.mp4';
const downloadBtn = document.querySelector('.download');
const abortBtn = document.querySelector('.abort');
const url = "video.mp4";
const downloadBtn = document.querySelector(".download");
const abortBtn = document.querySelector(".abort");

downloadBtn.addEventListener('click', fetchVideo);
downloadBtn.addEventListener("click", fetchVideo);

abortBtn.addEventListener('click', () => {
abortBtn.addEventListener("click", () => {
controller.abort();
console.log('Download aborted');
console.log("Download aborted");
});

function fetchVideo() {
fetch(url, { signal })
.then((response) => {
console.log('Download complete', response);
console.log("Download complete", response);
})
.catch((err) => {
console.error(`Download error: ${err.message}`);
Expand Down
10 changes: 5 additions & 5 deletions files/zh-cn/web/api/abortsignal/abort_event/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ slug: Web/API/AbortSignal/abort_event
在 {{domxref("EventTarget.addEventListener", "addEventListener()")}} 等方法中使用事件名称,或者设置一个事件处理器属性。

```js
addEventListener('abort', (event) => { })
onabort = (event) => { }
addEventListener("abort", (event) => {});
onabort = (event) => {};
```

## 事件类型
Expand All @@ -30,8 +30,8 @@ onabort = (event) => { }
const controller = new AbortController();
const signal = controller.signal;

signal.addEventListener('abort', () => {
console.log('Request aborted');
signal.addEventListener("abort", () => {
console.log("Request aborted");
});
```

Expand All @@ -41,7 +41,7 @@ signal.addEventListener('abort', () => {
const controller = new AbortController();
const signal = controller.signal;
signal.onabort = () => {
console.log('Request aborted');
console.log("Request aborted");
};
```

Expand Down
4 changes: 2 additions & 2 deletions files/zh-cn/web/api/abortsignal/abort_static/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ return controller.signal;
## 语法

```js
abort()
abort(reason)
abort();
abort(reason);
```

### 参数
Expand Down
4 changes: 2 additions & 2 deletions files/zh-cn/web/api/abortsignal/aborted/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ const signal = controller.signal;
//

if (signal.aborted) {
console.log('Request has been aborted');
console.log("Request has been aborted");
} else {
console.log('Request not aborted');
console.log("Request not aborted");
}
```

Expand Down
46 changes: 23 additions & 23 deletions files/zh-cn/web/api/abortsignal/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,21 @@ _**`AbortSignal`** 接口继续它父接口 {{domxref("EventTarget")}} 的方法
const controller = new AbortController();
const signal = controller.signal;

const url = 'video.mp4';
const downloadBtn = document.querySelector('.download');
const abortBtn = document.querySelector('.abort');
const url = "video.mp4";
const downloadBtn = document.querySelector(".download");
const abortBtn = document.querySelector(".abort");

downloadBtn.addEventListener('click', fetchVideo);
downloadBtn.addEventListener("click", fetchVideo);

abortBtn.addEventListener('click', () => {
abortBtn.addEventListener("click", () => {
controller.abort();
console.log('Download aborted');
console.log("Download aborted");
});

function fetchVideo() {
fetch(url, { signal })
.then((response) => {
console.log('Download complete', response);
console.log("Download complete", response);
})
.catch((err) => {
console.error(`Download error: ${err.message}`);
Expand All @@ -84,7 +84,7 @@ function fetchVideo() {
以下代码片段展示了如何成功地下载一个文件或者在五秒钟后处理一个超时的错误。注意,当出现超时时,`fetch()` promise 会以“`TimeoutError``DOMException` 拒绝。这允许代码区分超时(可能需要通知用户)和用户中止。

```js
const url = 'video.mp4';
const url = "video.mp4";

try {
const res = await fetch(url, { signal: AbortSignal.timeout(5000) });
Expand All @@ -94,7 +94,9 @@ try {
if (err.name === "TimeoutError") {
console.error("Timeout: It took more than 5 seconds to get the result!");
} else if (err.name === "AbortError") {
console.error("Fetch aborted by user action (browser stop button, closing tab, etc.");
console.error(
"Fetch aborted by user action (browser stop button, closing tab, etc.",
);
} else if (err.name === "TypeError") {
console.error("AbortSignal.timeout() method is not supported");
} else {
Expand All @@ -112,20 +114,18 @@ try {

```js
try {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), 5000)
const res = await fetch(url, { signal: controller.signal })
const body = await res.json()
}
catch (e) {
if (e.name === "AbortError") {
// Notify the user of abort.
// Note this will never be a timeout error!
} else {
// A network error, or some other problem.
console.log(`Type: ${e.name}, Message: ${e.message}`)
}

const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const res = await fetch(url, { signal: controller.signal });
const body = await res.json();
} catch (e) {
if (e.name === "AbortError") {
// Notify the user of abort.
// Note this will never be a timeout error!
} else {
// A network error, or some other problem.
console.log(`Type: ${e.name}, Message: ${e.message}`);
}
} finally {
clearTimeout(timeoutId);
}
Expand Down
4 changes: 2 additions & 2 deletions files/zh-cn/web/api/abortsignal/reason/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ if (signal.aborted) {
if (signal.reason) {
console.log(`Request aborted with reason: ${signal.reason}`);
} else {
console.log('Request aborted but no reason was given.');
console.log("Request aborted but no reason was given.");
}
} else {
console.log('Request not aborted');
console.log("Request not aborted");
}
```

Expand Down
10 changes: 5 additions & 5 deletions files/zh-cn/web/api/abortsignal/throwifaborted/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ slug: Web/API/AbortSignal/throwIfAborted
## 语法

```js
throwIfAborted()
throwIfAborted();
```

### 参数
Expand Down Expand Up @@ -57,7 +57,7 @@ async function waitForCondition(func, targetValue, { signal } = {}) {
一个基于 {{jsxref("Promise")}} 的 API 应该通过使用 `AbortSignal` abort {{domxref("AbortSignal.reason", "reason")}} 拒绝任何未敲定的 promise 来响应中止信号。例如,考虑以下 `myCoolPromiseAPI`,它接收一个信号并且返回一个 promise。如果 signal 已经中止或者检测到中止事件,则 promise 将被立刻拒绝。否则它将正常返回并且兑现。
```js
function myCoolPromiseAPI(/* … ,*/ {signal}) {
function myCoolPromiseAPI(/* … ,*/ { signal }) {
return new Promise((resolve, reject) => {
// If the signal is already aborted, immediately throw in order to reject the promise.
if (signal.aborted) {
Expand All @@ -68,7 +68,7 @@ function myCoolPromiseAPI(/* … ,*/ {signal}) {
// Call resolve(result) when done.

// Watch for 'abort' signals
signal.addEventListener('abort', () => {
signal.addEventListener("abort", () => {
// Stop the main operation
// Reject the promise wth the abort reason.
reject(signal.reason);
Expand All @@ -86,9 +86,9 @@ const signal = controller.signal;
startSpinner();

myCoolPromiseAPI({ /* … ,*/ signal })
.then((result) => { })
.then((result) => {})
.catch((err) => {
if (err.name === 'AbortError') return;
if (err.name === "AbortError") return;
showUserErrorMessage();
})
.then(() => stopSpinner());
Expand Down
6 changes: 4 additions & 2 deletions files/zh-cn/web/api/abortsignal/timeout_static/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ original_slug: Web/API/AbortSignal/timeout
## 语法

```js
timeout(time)
timeout(time);
```

### 参数
Expand Down Expand Up @@ -46,7 +46,9 @@ try {
if (err.name === "TimeoutError") {
console.error("Timeout: It took more than 5 seconds to get the result!");
} else if (err.name === "AbortError") {
console.error("Fetch aborted by user action (browser stop button, closing tab, etc.");
console.error(
"Fetch aborted by user action (browser stop button, closing tab, etc.",
);
} else if (err.name === "TypeError") {
console.error("AbortSignal.timeout() method is not supported");
} else {
Expand Down
4 changes: 2 additions & 2 deletions files/zh-cn/web/api/accelerometer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ If a feature policy blocks the use of a feature, it is because your code is inco
Acceleration is typically read in the {{domxref('Sensor.onreading')}} event callback. In the example below this occurs sixty times a second.

```js
let accelerometer = new Accelerometer({frequency: 60});
let accelerometer = new Accelerometer({ frequency: 60 });

accelerometer.addEventListener('reading', e => {
accelerometer.addEventListener("reading", (e) => {
console.log("Acceleration along the X-axis " + accelerometer.x);
console.log("Acceleration along the Y-axis " + accelerometer.y);
console.log("Acceleration along the Z-axis " + accelerometer.z);
Expand Down
Loading

0 comments on commit 6add031

Please sign in to comment.