0

I am following the sample shown here How to detect internet speed in Javascript? where an image with known file size is downloaded from the web and the speed is determined. For some reason I am not getting a result. My implementation is slightly different in that I am calling the JavaScript using InvokeScript and getting the value via ScriptNotify. My result value, however, is NaN. What can I do to fix this?

JavaScript

var imageAddr = "http://www.tranquilmusic.ca/images/cats/Cat2.JPG";
    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);

        window.external.notify("COT" + speedMbps);
    }

C#

private void RunTestButton_Click(object sender, RoutedEventArgs e)
    {
        object connectionType = Browser.InvokeScript("showResults");
    }

private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        string value = null;
        value = e.Value.ToString();

        TempResultTextBlock.Text = value;
    }
Community
  • 1
  • 1
Matthew
  • 3,976
  • 15
  • 66
  • 130
  • Not sure this is actually your problem, but `.toFixed(2)` turns your numbers into strings. You really ought to leave that out until the very end of your calculations and then only when you want to display a final result. – jfriend00 Apr 01 '14 at 05:27
  • I'll try that. Would I get some sort of syntax or other error using `.toString` as opposed to `.toFixed(2)`? – Matthew Apr 01 '14 at 05:33
  • I don't understand what you mean. You can leave off the `.toFixed()` everywhere you have it and just put it here: `window.external.notify("COT" + speedMbps.toFixed(2));`. Then, all your numbers stay numbers like they should be. – jfriend00 Apr 01 '14 at 05:35
  • I have implemented your suggestion with the same result. Am I downloading the image properly? I cannot determine what the error may be. – Matthew Apr 04 '14 at 20:20

0 Answers0