Skip to content

Commit fd18dfc

Browse files
committed
请求支持设置 timeout 最大请求时间(毫秒),若超出该时间,请求会自动终止
1 parent 63140cc commit fd18dfc

File tree

7 files changed

+38
-22
lines changed

7 files changed

+38
-22
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010

1111

1212

13+
# 5.5.0
14+
> 2022.08.23
15+
<font color=green>新增</font> 请求支持设置 `timeout` 最大请求时间(毫秒),若超出该时间,请求会自动终止
16+
17+
1318
# 5.4.1
1419
> 2022.03.07
1520
1. <font color=red>修复</font> 使用`onData`分段返回数据,请求报错时无法获取错误状态

dist/base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,9 @@ var AjaxBase = /** @class */ (function () {
565565
if (options.onProgress) {
566566
xhr.upload.onprogress = options.onProgress;
567567
}
568+
if (typeof options.timeout === 'number') {
569+
xhr.timeout = options.timeout;
570+
}
568571
// prettier-ignore
569572
xhr.send(params);
570573
if (cancelExecutor) {

dist/interface.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export interface IOptions extends IOptionsBase {
4949
}) => any;
5050
/** 简单请求(不添加默认请求头,除了外部传入的) */
5151
simple?: boolean;
52+
/** 最大请求时间(毫秒),若超出该时间,请求会自动终止 */
53+
timeout?: number;
5254
/** 自定义选项,用来传递值自定义处理逻辑 */
5355
[name: string]: any;
5456
}

lib/base.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ class AjaxBase {
641641
xhr.upload.onprogress = options.onProgress;
642642
}
643643

644+
if (typeof options.timeout === 'number') {
645+
xhr.timeout = options.timeout;
646+
}
647+
644648
// prettier-ignore
645649
xhr.send(
646650
params as Document | Blob | BufferSource | FormData | URLSearchParams | string | null

lib/interface.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export interface IOptions extends IOptionsBase {
5959
) => any;
6060
/** 简单请求(不添加默认请求头,除了外部传入的) */
6161
simple?: boolean;
62+
/** 最大请求时间(毫秒),若超出该时间,请求会自动终止 */
63+
timeout?: number;
6264
/** 自定义选项,用来传递值自定义处理逻辑 */
6365
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6466
[name: string]: any;

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "http-ajax",
3-
"version": "5.4.1",
3+
"version": "5.5.0",
44
"description": "Promise based HTTP client for the browser",
55
"keywords": [
66
"ajax",

0 commit comments

Comments
 (0)