playground.shiny.space/ajax.html
Lucas 94e6b88cbe Initial commit: Web development playground
- Frontend: HTML/CSS/JS demos and test pages
- Backend: Go API with SQLite database
- Features: AJAX calls, items management, random number generation
- Database: SQLite with items table for CRUD operations

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-14 01:38:32 +02:00

83 lines
No EOL
3.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX Demo</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>AJAX Demo</h1>
<p>This page demonstrates asynchronous loading. The UI loads immediately, then makes a slow API call in the background.</p>
<div class="section">
<h3>Slow Response Test</h3>
<p>Click the button to make a request to the slow endpoint (1 second delay):</p>
<button id="loadBtn" onclick="loadSlowResponse()">Load Slow Response</button>
<button id="clearBtn" onclick="clearResponse()">Clear</button>
<div id="responseField" class="response-field centered">
<span class="loading">Loading...</span>
</div>
<div id="timestamp" class="timestamp"></div>
<div class="info">
The page loads instantly and automatically starts loading the slow response. The API response takes ~1 second to arrive.
</div>
</div>
<div class="back-link">
<a href="index.html">← Back to Home</a>
</div>
<script>
let requestStartTime;
async function loadSlowResponse() {
const responseField = document.getElementById('responseField');
const loadBtn = document.getElementById('loadBtn');
const timestampDiv = document.getElementById('timestamp');
// Disable button and show loading state
loadBtn.disabled = true;
responseField.innerHTML = '<span class="loading">Loading... (this takes ~1 second)</span>';
requestStartTime = Date.now();
timestampDiv.innerHTML = `Request started at: ${new Date().toLocaleTimeString()}`;
try {
const response = await fetch('/api/slowtest');
const data = await response.text();
const elapsed = Date.now() - requestStartTime;
responseField.innerHTML = `<span class="response large">Response: ${data}</span>`;
timestampDiv.innerHTML = `Request completed at: ${new Date().toLocaleTimeString()} (took ${elapsed}ms)`;
} catch (error) {
responseField.innerHTML = '<span class="error">Error loading response</span>';
timestampDiv.innerHTML = `Error at: ${new Date().toLocaleTimeString()}`;
console.error('Error:', error);
} finally {
loadBtn.disabled = false;
}
}
function clearResponse() {
const responseField = document.getElementById('responseField');
const timestampDiv = document.getElementById('timestamp');
responseField.innerHTML = '<span class="loading">Click "Load Slow Response" to test the API</span>';
timestampDiv.innerHTML = '';
}
// Show that the page loaded immediately and start the slow request
document.addEventListener('DOMContentLoaded', function() {
console.log('Page loaded immediately at:', new Date().toLocaleTimeString());
loadSlowResponse();
});
</script>
</body>
</html>