|
HTML, CSS 및 자바스크립트
옵션 열기
이 튜토리얼에서는 JavaScript를 사용하여 인터넷 속도 테스트를 코딩하는 방법을 배웁니다. 이 비디오에서는 unsplash API에서 이미지를 다운로드하고 크기에 따라 이미지 다운로드 시간을 계산하여 인터넷 속도를 감지합니다. & 영상 내용 :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Day #40 - Detect Internet Speed | AsmrProg</title>
<style>
let startTime, endTime;
let imageSize = "";
let image = new Image();
let bitSpeed = document.getElementById("bits"),
kbSpeed = document.getElementById("kbs"),
mbSpeed = document.getElementById("mbs"),
info = document.getElementById("info");
let totalBitSpeed = 0;
let totalKbSpeed = 0;
let totalMbSpeed = 0;
let numTests = 1;
let testCompleted = 0;
// Get random image from unsplash.com
let imageApi = "https://source.unsplash.com/random?topic=nature";
// When image loads
image.xxxxonload = async function () {
endTime = new Date().getTime();
// Get image size
await fetch(imageApi).then((response) => {
imageSize = response.headers.get("content-length");
calculateSpeed();
});
};
// Function to calculate speed
function calculateSpeed() {
// Time taken in seconds
let timeDuration = (endTime - startTime) / 1000;
// Total bits
let loadedBits = imageSize * 8;
let speedInBts = loadedBits / timeDuration;
let speedInKbs = speedInBts / 1024;
let speedInMbs = speedInKbs / 1024;
totalBitSpeed += speedInBts;
totalKbSpeed += speedInKbs;
totalMbSpeed += speedInMbs;
testCompleted++;
// If all tests completed (we get 5 image then calculate average)
if (testCompleted === numTests) {
let averageSpeedInBps = (totalBitSpeed / numTests).toFixed(2);
let averageSpeedInKbps = (totalKbSpeed / numTests).toFixed(2);
let averageSpeedInMbps = (totalMbSpeed / numTests).toFixed(2);
// Display average speeds
bitSpeed.innerHTML += `${averageSpeedInBps}`;
kbSpeed.innerHTML += `${averageSpeedInKbps}`;
mbSpeed.innerHTML += `${averageSpeedInMbps}`;
info.innerHTML = "Test Completed!";
} else {
// Run the next test
startTime = new Date().getTime();
image.src = imageApi;
}
}
// Initial function to start tests
const init = async () => {
info.innerHTML = "Testing...";
startTime = new Date().getTime();
image.src = imageApi;
};
// Run tests when window loads
window.xxxxonload = () => {
for (let i = 0; i < numTests; i++) {
init();
}
};
</style>
</head>
<body>
<div class="container">
<img src="speedometer.png">
<p id="info"><span>...</span></p>
<p id="mbs"><span>Speed In Mbs: </span></p>
<p id="kbs"><span>Speed In Kbs: </span></p>
<p id="bits"><span>Speed In Bits: </span></p>
</div>
<script src="index.js"></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700;800&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body{
height: 100vh;
background: linear-gradient(#27272a 50%, #075985 50%);
display: flex;
align-items: center;
justify-content: center;
}
.container{
background-color: #fff;
display: flex;
flex-direction: column;
align-items: center;
width: 350px;
padding: 0 2em 1em;
border-radius: 1em;
font-size: 1.2em;
line-height: 1.6em;
box-shadow: 0 1.6em 2.4em rgba(40, 4, 67, 0.3);
}
.container img{
width: 100%;
}
p{
font-weight: 400;
color: #747497;
}
span{
font-weight: 400;
color: #090931;
}
let startTime, endTime;
let imageSize = "";
let image = new Image();
let bitSpeed = document.getElementById("bits"),
kbSpeed = document.getElementById("kbs"),
mbSpeed = document.getElementById("mbs"),
info = document.getElementById("info");
let totalBitSpeed = 0;
let totalKbSpeed = 0;
let totalMbSpeed = 0;
let numTests = 1;
let testCompleted = 0;
// Get random image from unsplash.com
let imageApi = "https://source.unsplash.com/random?topic=nature";
// When image loads
image.xxxxonload = async function () {
endTime = new Date().getTime();
// Get image size
await fetch(imageApi).then((response) => {
imageSize = response.headers.get("content-length");
calculateSpeed();
});
};
// Function to calculate speed
function calculateSpeed() {
// Time taken in seconds
let timeDuration = (endTime - startTime) / 1000;
// Total bits
let loadedBits = imageSize * 8;
let speedInBts = loadedBits / timeDuration;
let speedInKbs = speedInBts / 1024;
let speedInMbs = speedInKbs / 1024;
totalBitSpeed += speedInBts;
totalKbSpeed += speedInKbs;
totalMbSpeed += speedInMbs;
testCompleted++;
// If all tests completed (we get 5 image then calculate average)
if (testCompleted === numTests) {
let averageSpeedInBps = (totalBitSpeed / numTests).toFixed(2);
let averageSpeedInKbps = (totalKbSpeed / numTests).toFixed(2);
let averageSpeedInMbps = (totalMbSpeed / numTests).toFixed(2);
// Display average speeds
bitSpeed.innerHTML += `${averageSpeedInBps}`;
kbSpeed.innerHTML += `${averageSpeedInKbps}`;
mbSpeed.innerHTML += `${averageSpeedInMbps}`;
info.innerHTML = "Test Completed!";
} else {
// Run the next test
startTime = new Date().getTime();
image.src = imageApi;
}
}
// Initial function to start tests
const init = async () => {
info.innerHTML = "Testing...";
startTime = new Date().getTime();
image.src = imageApi;
};
// Run tests when window loads
window.xxxxonload = () => {
for (let i = 0; i < numTests; i++) {
init();
}
};
https://github.com/AsmrProg-YT/100-days-of-javascript/blob/master/Day%20%2340%20-%20Detect%20Internet%20Speed/style.css
13:14 - 최종 결과
구독: https://www.youtube.com/@AsmrProg/featured
HTML, CSS 및 자바스크립트
|
첫댓글 https://www.w3schools.com/css/tryit.asp?filename=trycss_default
https://morioh.com/a/eee0a648e119/responsive-ecommerce-clothing-website-design-using-html-css-and-javascr