4

XMLHTTPRequest を使用して「ダウンローダー」を実装しました。これは、javascript を使用してファイルとファイルの一部をダウンロードするために使用します。テストのために、そしておそらく実際のシナリオのために、このダウンローダが必要とする帯域幅を制限したいと思います。

私が念頭に置いているオプションの 1 つは、setTimeout を使用してダウンローダーを何度も停止および開始することです。これにより、次の http ブロックのダウンロードが停止します。でもこれはかなりヤバい…

チャンクの次のリクエストを何らかの形で停止できますか? おそらく、onreadystatechange イベントに遅延を入れることによって可能です。コードは次のとおりです。

download:function (start, end) {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", this.url, true);

    if (end) {
        xhr.setRequestHeader("Range", "bytes=" + start + "-" + end);
    }
    else {
        if (start && start > 0) {
            xhr.setRequestHeader("Range", "bytes=" + start);
        }
        else {
            start = 0;
        }
    }
    // Hack to pass bytes through unprocessed.
    xhr.overrideMimeType('text/plain; charset=x-user-defined');

    thi$ = this;
    xhr.onreadystatechange = function() {
        if (this.stopped) {
            xhr.abort();
        }
        if (typeof this.index == "undefined")
            this.index = 0;

        if (this.readyState >= 3 && this.status == 200) {
            var content = this.responseText;
            thi$.handleContent( content.substring(this.index), this.index);
            this.index = content.length;
        }
    }

    xhr.send(null);
}
4

0 に答える 0