Get estimate of connection speed with Javascript action

Hi all, I am trying to get an estimate of the users’ connection speed. To do this I have created a small JS action that looks like this: /** * @returns {string} */ function JS_Alternative() { // BEGIN USER CODE var imageAddr = "" + "?n=" + Math.random(); var startTime, endTime; var downloadSize = 5616998; var download = new Image(); download.onload = function () { endTime = (new Date()).getTime(); showResults(); } startTime = (new Date()).getTime(); download.src = imageAddr; function showResults() { var duration = (endTime - startTime) / 1000; //Math.round() var bitsLoaded = downloadSize * 8; var speedBps = (bitsLoaded / duration).toFixed(2); var speedKbps = (speedBps / 1024).toFixed(2); var speedMbps = (speedKbps / 1024).toFixed(2); console.log(speedKbps); return speedKbps.toString; /* alert("Your connection speed is: \n" + speedBps + " bps\n" + speedKbps + " kbps\n" + speedMbps + " Mbps\n"); */ } // END USER CODE } The code runs, but there is nothing returned. If I run this code with an online JS editor, the result is as expected. What is going wrong here? Funny thing is that if I uncomment the alert part, this is shown correctly in the browser.
1 answers

The alert you can only use AFTER the call to the showResults function, WITHIN the onload eventhandler.

Also the speed variables are to be declared outside the showResults function, otherwise they are not available in the alert.

After those changes, I succeeded in an alert with non zero results.