Skip to content

Instantly share code, notes, and snippets.

@huanggm
Created April 14, 2020 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huanggm/b9b3c798121e7468be706d16c54a5ccb to your computer and use it in GitHub Desktop.
Save huanggm/b9b3c798121e7468be706d16c54a5ccb to your computer and use it in GitHub Desktop.
测试网速可以使用XMLHttpRequest对象的onprogress事件实现
function speedtest() {
var xmlhttp = new XMLHttpRequest(),
method = "GET",
url = "http://109.123.87.183/speedtest.256mb";
const start = Date.now();
xmlhttp.open(method, url, true);
xmlhttp.onprogress = function (e) {
const dnow = Date.now();
const miao = (dnow - start) / 1000;
const mb = 1024 * 1024;
console.log(
miao + "s",
"下载量:",
e.loaded / mb + "MB",
"总量:",
e.total / mb + "MB",
"网速:",
e.loaded / miao / mb + "MB/s"
);
};
xmlhttp.send();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment