开发者

Using jQuery.get() to measure client bandwidth

开发者 https://www.devze.com 2023-03-19 00:54 出处:网络
I am using the following script do determine the downlink bandwidth of a user to serve content accordingly:

I am using the following script do determine the downlink bandwidth of a user to serve content accordingly:

    var bandwidthCheckStart = new Date().getTime();
    var bandwidthCheckEnd;
    $.ajax({
        url: xBandwidthCheckFile,
        async: false,
        cache: false,
        success: function(clipXmlRequest){
            bandwidthCheckEnd = new Date().getTime();
        },
        error: function() {
            xSendDebugMessage("Could not get \""+xBandwidthCheckFile+"\" from server.", "warning");
        }
    });
    var bandwidthCheckDuration = bandwidthCheckEnd - bandwidthCheckStart;

However, I feel like this method is not reliable, since it doesnt consider the time needed to initiate th开发者_Python百科e file transfer. (We could probably minimize the effects by using a larger file to check the bandwidth but I want it to be 10kb max.)

Is there any way to determine the time the request starts to be answered or do you have another more reliable way in mind?


Use your code, monitor the incidental connections too, and build a profile for your site users. It's far from ideal, but that may be the only way right now.

You could start lobbying W3C and browsers to add that feature... Let me know if you do!


You should calculate the bandwidthCheckDuration inside the success function. Otherwise it will be calculated before the transmissions has ended.

success: function(clipXmlRequest){
    bandwidthCheckEnd = new Date().getTime();
    alert(bandwidthCheckEnd - bandwidthCheckStart); 
}


I recommend using Navigation Timing.

0

精彩评论

暂无评论...
验证码 换一张
取 消