-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (38 loc) · 1.51 KB
/
index.js
File metadata and controls
47 lines (38 loc) · 1.51 KB
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
46
47
const imageAddr = "https://wallpaperswide.com/download/shadow_of_the_tomb_raider_2018_puzzle_video_game-wallpaper-7680x4800.jpg" + "?n=" + Math.random();
let startTime, endTime;
const downloadSize = 5616998; //5.36Mb
const roundedDecimals = 2;
const bytesInAKilobyte = 1024;
function showResults() {
var duration = (endTime - startTime) / 1000;
var bitsLoaded = downloadSize * 8;
var speedBps = ( bitsLoaded / duration ).toFixed(roundedDecimals);
var displaySpeed = speed(speedBps);
var results = displaySpeed.value + " " + displaySpeed.units;
$('.icon').hide()
$('.loader').fadeOut('fast')
$('.button-text').css("margin", 0)
$('.button-text').text(results);
}
function speed( bitsPerSecond ){
var KBps = (bitsPerSecond / bytesInAKilobyte).toFixed(roundedDecimals);
if ( KBps <= 1 ) return { value: bitsPerSecond, units: "Bps" };
var MBps = (KBps / bytesInAKilobyte).toFixed(roundedDecimals);
if ( MBps <= 1 ) return { value: KBps, units: "KBps" };
else return { value: MBps, units: "MBps" };
}
$('#starttest').on('click', function(){
$('#starttest').addClass('active')
$('.button-text').text('Downloading...');
$('#starttest').prop('disabled', true);
$('.icon').hide()
$('.loader').fadeIn('fast')
const download = new Image();
startTime = undefined, endTime = undefined
download.onload = function () {
endTime = (new Date()).getTime();
showResults();
}
startTime = (new Date()).getTime();
download.src = imageAddr;
})