使用JavaScript计算速度返回NaN

时间:2014-04-01 05:15:41

标签: c# javascript html5 windows-phone-8 internet-explorer-10

我正在关注此处显示的示例How to detect internet speed in Javascript?,其中从网络下载具有已知文件大小的图像并确定速度。出于某种原因,我没有得到结果。我的实现略有不同,因为我使用InvokeScript调用JavaScript并通过ScriptNotify获取值。但是,我的结果值是NaN。我该怎么做才能解决这个问题?

的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;
    }

0 个答案:

没有答案