高频率点击下载按钮,进而引起下载方法未完成再次高频率触发,导致浏览器阻塞甚至崩溃的问题
下面解决这个问题
<el-button type="primary" :loading="downloading" @click="downloadImg">下载原图</el-button>
downloadImg(){
this.downloading = true //下载按钮的状态控制
let x = new XMLHttpRequest();
x.open("GET", this.downloadObj.cloud_name, true); //图片地址
x.responseType = 'blob';
x.onload = (e)=>{
var url = window.URL.createObjectURL(x.response)
var a = document.createElement('a');
a.href = url
a.download = ''
a.click()
}
x.send();
//监听onreadystatechange 判断返回值以改变状态(下载完成,恢复可点击状态)
x.onreadystatechange = ()=>{
if(x.readyState === 4){ //请求已完成,且响应已就绪
this.downloading = false //下载状态复原
}
如此如此,便解决了上述这个问题。