Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

Get internet speed in React using image download

$
0
0

I am trying to calculate the internet speed but I get a small value. This is the code:

useEffect(() =>     var imageAddr = "../../img/noventi-logo.png" +"?n=" + Math.random();    var startTime: number, endTime;    var downloadSize = 200000;    var download = new Image();    startTime = (new Date()).getTime();    download.src = imageAddr;    window.setTimeout(      download.onload = function () {        endTime = (new Date()).getTime();        showResults(endTime, startTime, downloadSize);    }, 1);,[]) function showResults(endTime: number, startTime: number, downloadSize: number) {    var duration = (endTime - startTime) / 1000;    var bitsLoaded = downloadSize * 8;    var speedBps: any = (bitsLoaded / duration).toFixed(2);    var speedKbps: any = (speedBps / 1024).toFixed(2);    var speedMbps = (speedKbps / 1024).toFixed(2);    alert("Your connection speed is: \n" +           speedBps +" bps\n"   +           speedKbps +" kbps\n" +           speedMbps +" Mbps\n" );}

And as a result I get only values like 0,6 Mbps.

PLEASE HELP ME


Viewing all articles
Browse latest Browse all 6287

Trending Articles