Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Internet Speed Checker JavaScript

//JUST AN EXAMPLE, PLEASE USE YOUR OWN PICTURE!
var imageAddr = "http://www.kenrockwell.com/contax/images/g2/examples/31120037-5mb.jpg"; 
var downloadSize = 4995374; //bytes

function ShowProgressMessage(msg) {
    if (console) {
        if (typeof msg == "string") {
            console.log(msg);
        } else {
            for (var i = 0; i < msg.length; i++) {
                console.log(msg[i]);
            }
        }
    }
    
    var oProgress = document.getElementById("progress");
    if (oProgress) {
        var actualHTML = (typeof msg == "string") ? msg : msg.join("<br />");
        oProgress.innerHTML = actualHTML;
    }
}

function InitiateSpeedDetection() {
    ShowProgressMessage("Loading the image, please wait...");
    window.setTimeout(MeasureConnectionSpeed, 1);
};    

if (window.addEventListener) {
    window.addEventListener('load', InitiateSpeedDetection, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', InitiateSpeedDetection);
}

function MeasureConnectionSpeed() {
    var startTime, endTime;
    var download = new Image();
    download.onload = function () {
        endTime = (new Date()).getTime();
        showResults();
    }
    
    download.onerror = function (err, msg) {
        ShowProgressMessage("Invalid image, or error downloading");
    }
    
    startTime = (new Date()).getTime();
    var cacheBuster = "?nnn=" + startTime;
    download.src = imageAddr + cacheBuster;
    
    function showResults() {
        var duration = (endTime - startTime) / 1000;
        var bitsLoaded = downloadSize * 8;
        var speedBps = (bitsLoaded / duration).toFixed(2);
        var speedKbps = (speedBps / 1024).toFixed(2);
        var speedMbps = (speedKbps / 1024).toFixed(2);
        ShowProgressMessage([
            "Your connection speed is:", 
            speedBps + " bps", 
            speedKbps + " kbps", 
            speedMbps + " Mbps"
        ]);
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js for in 10 
Javascript :: react js nesting 
Javascript :: javascript toutcstring 
Javascript :: iframe reload parent 
Javascript :: javascript vue.js right click 
Javascript :: users api testing 
Javascript :: javascript check if dom element 
Javascript :: iframe reload parent page 
Javascript :: javascript credit card validation 
Javascript :: toast angular 
Javascript :: how to debug jest test vscode 
Javascript :: mongodb sort query 
Javascript :: disable key enter react-hook-form 
Javascript :: javascript force precision to 2 decimal numbers 
Javascript :: i18n react get current language 
Javascript :: replace space with hyphen/dash javascript 
Javascript :: useref 
Javascript :: emotion react 
Javascript :: js isset 
Javascript :: post message in iframe javascript 
Javascript :: multiple click events in react 
Javascript :: javaSript string first words to upper case 
Javascript :: convert string number with commas decimal to number javascript 
Javascript :: javascript shuffle string 
Javascript :: user focus on tab javascript 
Javascript :: how to sort an array of objects by two fields in javascript 
Javascript :: javascript toPrecision() Method 
Javascript :: electron no resize 
Javascript :: JSON Web Token (JWT) set expire time in node js 
Javascript :: javascript canvas grayscale 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =