Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get client connection speed

Status
Not open for further replies.

ccddmm

Programmer
Mar 23, 2006
7
US
I am having questions of getting the client connection speed in exclusively Javascript.
I searched online some of them when user clicks a link, then pop up the connection speed.
I do not want to add any html code for getting the speed. Seems after the image finished loading,
the getConnectionSpeed function is still running, and at the mean time, the next assign command
strConnectSpeed = connectionSpeed; is executed, so the strConnectSpeed is always undefined.
I tried to put a while loop saying while (strConnectSpeed=="undefined"); this time the while
loop block everything and slows everything, and the next command afer while does not execute
until the while loop is done. These two cases seem contradictory to me, how to explain this?
Anyone can help me to get the strConnectSpeed assigned with the value?

///////////////////////////////////////////////////////////
<html><head>

</head><BODY>
<script language=javascript>
<!--

var connectionSpeed = 0;
var strConnectSpeed = "undefined";

drawCSImage('email_newsletter.gif',35053 , 'width=1 height=1');
setTimeout("checkSpeed()", 5000);

//the following commands get executed before the setTimeout
//so the strConnectSpeed is always "undefined"
//How can I get strConnectSpeed assigned value and use it?
//I tried while loop, but it slows everything.
strConnectSpeed = connectionSpeed;
alert(strConnectSpeed );
/////////////////////////////////////////////



function drawCSImage( fileLocation, fileSize, imgTagProperties ) {
start = (new Date()).getTime();
document.write('');
return;
}

function getConnectionSpeed( start, fileSize ) {
end = (new Date()).getTime();
return (Math.floor((((fileSize * 8) / ((end - start) / 1000)) / 1024) * 10) / 10);
}


function checkSpeed()
{
strConnectSpeed = connectionSpeed;
alert(strConnectSpeed );
}


//--></script>
</body>
</html>

 
You have the alert in your checkSpeed() function, so you don't need it in the main body like you have now.

Lee
 
I put the alert in my checkSpeed function for testing. What I really want is getting the alert in the main body to work.
 
Since you're not actually assigning another value to the variable for 5 seconds, you're getting the information in the exact order you programmed it. The value is undefined for the first 5 seconds, and if you check it at any point before those 5 seconds are up, you'll only get "undefined".

The point of using setTimeout is to create a delay before some code is run, and it's the delayed code that changes your strConnectSpeed value.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top