-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathabort_fetch_request.js
40 lines (32 loc) · 1 KB
/
abort_fetch_request.js
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
https://stackoverflow.com/questions/31061838/how-do-i-cancel-an-http-fetch-request
https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
https://github.com/mo/abortcontroller-polyfill#readme
<script>
// Create an instance.
const controller = new AbortController()
const signal = controller.signal
/*
// Register a listenr.
signal.addEventListener("abort", () => {
console.log("aborted!")
})
*/
function beginFetching() {
console.log('Now fetching');
var urlToFetch = "https://httpbin.org/delay/3";
fetch(urlToFetch, {
method: 'get',
signal: signal,
})
.then(function(response) {
console.log(`Fetch complete. (Not aborted)`);
}).catch(function(err) {
console.error(` Err: ${err}`);
});
}
function abortFetching() {
console.log('Now aborting');
// Abort.
controller.abort()
}
</script>