Created
April 14, 2020 08:47
-
-
Save huanggm/b9b3c798121e7468be706d16c54a5ccb to your computer and use it in GitHub Desktop.
测试网速可以使用XMLHttpRequest对象的onprogress事件实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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