-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.html
More file actions
57 lines (50 loc) · 1.64 KB
/
test.html
File metadata and controls
57 lines (50 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>脚本调试</div>
</body>
<script>
function retryRequest(url, maxRetries, delay, callback) {
let retries = 0;
function makeRequest() {
// 发送接口请求
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error('Request failed');
}
return response.json();
})
.then((data) => {
// 请求成功
callback(data);
})
.catch((error) => {
// 请求失败
if (retries < maxRetries) {
retries++;
setTimeout(makeRequest, delay);
} else {
// 达到最大重试次数,处理失败情况
console.error('Max retries reached. Request failed.');
// 可以执行其他操作,如提示用户或记录日志
}
});
}
makeRequest();
}
// 使用示例
const apiUrl = 'https://example.com/api/data';
const maxRetries = 5; // 最大重试次数
const retryDelay = 2000; // 重试间隔时间(毫秒)
retryRequest(apiUrl, maxRetries, retryDelay, (data) => {
// 处理请求成功后的数据
console.log('Request succeeded:', data);
});
</script>
</html>