用<link>标签检测
function getStatus(url:string){
return new Promise(resolve,reject){
let link = document.createElement('link')
link.rel="stylesheets",
link.type="text/css"
link.href=url;
link.onload=function(){
resolve(true)
}
link.onerror=function(){
resolve(false)
}
document.body.appendChild(link)
}
}
2.计算网络延迟
function getLoadTime(url:string){
return new Promise(resolve,reject){
let img= document.createElement('img')
img.style.display=none
img.src=`${url}/?v=${Math()*random()}`;
const startTime = new Date()
link.onload=function(){
const endTime=new Date()
resolve(endTime.getTIme()-startTime .getTime())
}
link.onerror=function(){
const endTime=new Date()
resolve(endTime.getTIme()-startTime .getTime())
}
document.body.appendChild(img)
}
}