-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathping.js
54 lines (48 loc) · 1.34 KB
/
ping.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
46
47
48
49
50
51
52
53
54
async function pingHost() {
var protocol = document.getElementById("protocol").value;
var host = document.getElementById("host").value;
if (!host) {
alert("host cannot be empty");
return;
}
var count = parseInt(document.getElementById("count").value, 10);
if (isNaN(count) || count === 0) {
count = 1;
}
var url = protocol + "://" + host;
async function makePingRequest() {
var startTime = Date.now();
await fetch(url, {
method: "GET",
mode: "no-cors"
})
.then(function(response) {
var endTime = Date.now();
var pingRate = endTime - startTime;
// Create a new row in the table
var table = document.getElementById("pingTable");
var row = table.insertRow(-1);
var hostCell = row.insertCell(0);
var pingRateCell = row.insertCell(1);
// Set the values in the table cells
hostCell.innerHTML = host;
pingRateCell.innerHTML = pingRate + " ms";
// Display the table
table.style.display = "table";
return true;
})
.catch(function(error) {
throw error.message;
});
}
// Start the ping requests in a loop
for (var i = 0; i < count; i++) {
try {
await makePingRequest();
} catch (error) {
console.log(error);
alert("Error: " + error);
break;
}
}
}