Request a delayed response to test timeout handling with AbortController.
Code
Utilitiesconst controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000);
const start = Date.now();
const response = await fetch(`https://httpbin.org/delay/${delaySeconds}`, {
signal: controller.signal
});
clearTimeout(timeoutId);
const duration = Date.now() - start;
({ duration: Math.round(duration / 1000), data: await response.json() });Parameters
Delay in seconds (max 10).
Browser·fetch() may be limited by CORS
Timeout Pattern
// Abort after 3 seconds
const controller = new AbortController();
setTimeout(() => controller.abort(), 3000);
try {
const response = await fetch(url, { signal: controller.signal });
} catch (err) {
if (err.name === 'AbortError') {
console.log('Request timed out');
}
}
More JavaScript Snippets
Add Query Parameter
Add or update a query parameter in a URL string.
Bearer Token Authentication
Make an authenticated HTTP request using Bearer token for JWT or OAuth.
Check Cloudflare Cache Status
Check if a resource is served from Cloudflare's cache by inspecting the CF-Cache-Status header.
CORS Cross-Origin Request
Make a cross-origin HTTP request with CORS headers inspection.
Download Binary File
Download an image or binary file and convert it to a blob or base64.
Fetch Error Details
Extract status, headers and body from an HTTP error response.