开始测速
关于实时网络测速工具
此工具用于测量您当前互联网连接的实时速度。它测量以下指标:
-
Ping:服务器响应时间。
-
下载速度:从服务器下载数据的速度。
-
上传速度:上传数据到服务器的速度。
-
延迟:互联网连接中的往返时间。
此工具可用于诊断网络问题、确定连接速度是否达到预期标准以及监控网络性能。
CSS代码 (style.css)
css
body {font-family: Arial, sans-serif;
}.container {max-width: 500px;padding: 20px;margin: 0 auto;
}h1 {text-align: center;
}start-button {display: block;margin: 0 auto;padding: 10px 20px;border: 1px solid 000;border-radius: 5px;background-color: 000;color: fff;cursor: pointer;
}.results-container {margin-top: 20px;
}.result-item {display: flex;justify-content: space-between;margin-bottom: 10px;
}.result-item label {font-weight: bold;
}.result-item span {color: 000;
}.info-container {margin-top: 20px;
}JavaScript代码 (script.js)
javascript
const startButton = document.getElementById('start-button');
const results = document.getElementById('results');const pingValue = document.getElementById('ping-value');
const downloadValue = document.getElementById('download-value');
const uploadValue = document.getElementById('upload-value');
const latencyValue = document.getElementById('latency-value');let pingTimer;const startTest = () => {startButton.disabled = true;pingTimer = setInterval(pingTest, 1000);startDownloadTest();startUploadTest();
};const pingTest = () => {const startTime = Date.now();fetch('https://google.com', {method: 'HEAD'}).then(response => {const endTime = Date.now();pingValue.innerText = `${endTime - startTime}ms`;}).catch(error =>{pingValue.innerText = '无法连接';});
};const startDownloadTest = () => {const startTime = Date.now();fetch('https://speed.hetzner.de/100MB.bin').then(response => {const endTime = Date.now();const downloadSpeed = ((100 1024 1024) / (endTime - startTime)) 8;downloadValue.innerText = `${downloadSpeed.toFixed(2)} Mbps`;}).catch(error => {downloadValue.innerText = '无法下载';});
};const startUploadTest = () => {const startTime = Date.now();const formData = new FormData();formData.append('file', createRandomFile());fetch('https://speedtest.hetzner.de/upload', {method: 'POST',body: formData}).then(response => {const endTime = Date.now();const uploadSpeed = ((100 1024 1024) / (endTime - startTime)) 8;uploadValue.innerText = `${uploadSpeed.toFixed(2)} Mbps`;}).catch(error => {uploadValue.innerText = '无法上传';});
};const createRandomFile = () => {const size = 100 1024 1024;const buffer = new ArrayBuffer(size);return new File([buffer], 'random.bin');
};