-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
45 lines (38 loc) · 1.34 KB
/
scripts.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
41
42
43
44
45
//API link for random images: https://source.unsplash.com/random?topics=nature
let startTime, endTime;
let imageSize = "";
let image = new Image();
let bitOutput = document.getElementById("bits");
let kboutput = document.getElementById("kbs");
let mboutput = document.getElementById("mbs");
//Gets random image from unsplash.com
let imageLink = "https://source.unsplash.com/random?topics=nature";
//When image loads
image.onload = async function () {
endTime = new Date().getTime();
//Get image size
await fetch(imageLink).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 bit speed in 3 form
let loadedBits = imageSize * 8;
let speedInBps = (loadedBits / timeDuration).toFixed(2);
let speedInKbps = (speedInBps / 1024).toFixed(2);
let speedInMbps = (speedInKbps / 1024).toFixed(2);
alert(`Chulindra speed Detected : ${speedInMbps} mbps`);
bitOutput.innerHTML += `${speedInBps}`;
kboutput.innerHTML += `${speedInKbps}`;
mboutput.innerHTML += `${speedInMbps}`;
}
//Initial
const init = async () => {
startTime = new Date().getTime();
image.src = imageLink;
};
window.onload = () => init();