Advertisement
Nurrohman_rex

check internet connection

Nov 10th, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. function checkSpeed(){
  2. var internetSpeedTest = {
  3. run: function(callback) {
  4. var startTime = new Date().getTime();
  5. fetch(location.pathname, { method: "HEAD" })
  6. .then(response => {
  7. var endTime = new Date().getTime();
  8. var duration = (endTime - startTime);
  9. var latency = duration;
  10. callback(latency);
  11. })
  12. .catch(error => {
  13. console.error("Error during speed test:", error);
  14. callback(null);
  15. });
  16. }
  17. };
  18.  
  19. internetSpeedTest.run(function(latency) {
  20. if (latency !== null) {
  21. console.log("Latensi Koneksi: " + latency + " ms");
  22. if (latency < 500) {
  23. console.log("Koneksi Cepat");
  24. } else if (latency < 800) {
  25. console.log("Koneksi Menengah");
  26. } else {
  27. console.log("Koneksi Lambat");
  28. }
  29. } else {
  30. console.log("Tidak dapat mengukur kecepatan koneksi.");
  31. }
  32. });
  33. }
  34.  
  35.  
  36.  
  37. let isAlertShown = false;
  38. setInterval(function() {
  39. if (!window.navigator.onLine && !isAlertShown) {
  40. isAlertShown = true;
  41. Swal.fire({
  42. title: "Tidak ada koneksi internet",
  43. text: "Pastikan koneksi internet-mu tersambung dan coba kembali",
  44. icon: "warning",
  45. showCancelButton: false,
  46. showConfirmButton: false,
  47. confirmButtonColor: "#3085d6",
  48. allowOutsideClick: false,
  49. allowEscapeKey: false,
  50. });
  51. } else if (window.navigator.onLine && isAlertShown) {
  52. Swal.close();
  53. isAlertShown = false;
  54. }
  55. }, 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement