下载按钮禁止重复点击

This commit is contained in:
BlackTeay 2023-03-28 15:48:48 +08:00
parent 79d3f38552
commit 5a8fcf8b57

View File

@ -28,7 +28,7 @@
*/
function appendDownloadEle(videoJQ, title, videoSrc) {
let downloadIdAttrName = videoJQ.attr("id") + "_download";
var downloading=false
var downloading = false
console.log(downloadIdAttrName)
if ($("#" + downloadIdAttrName).length <= 0) {
// 【下载】按钮
@ -54,12 +54,12 @@
// 为下载按钮绑定单击事件下载视频
downloadJQ.on("click", function (event) {
// console.log("下载视频:" + $(this).attr("id") + ", href=" + $(this).attr("href"));
if(downloading==true){
console.log("视频下载中,请勿再次点击")
if (downloading == true) {
console.log("视频下载中,请勿再次点击")
return false;
}else{
downloading=true
} else {
downloading = true
}
// 阻止 a 标签默认行为
event.preventDefault();
@ -72,21 +72,22 @@
xhr.open('GET', downloadUrl, true);
xhr.responseType = 'blob';
xhr.onreadystatechange = function(){
console.log("READYSTATE"+ xhr.readyState);
if(this.status == 200 && this.readyState == 4){
const downloadLink = document.createElement('a');
xhr.onreadystatechange = function () {
console.log("READYSTATE" + xhr.readyState);
if (this.status == 200 && this.readyState == 4) {
const downloadLink = document.createElement('a');
downloadLink.href = window.URL.createObjectURL(new Blob([xhr.response]));
downloadLink.download = title + '.mp4';
document.body.appendChild(downloadLink);
downloadLink.click();
downloadLink.remove();
downloading=false;
}else if(this.status == 404){
console.log("视频不存在");
}
downloading = false;
} else if (this.status == 404) {
console.log("视频不存在");
downloading = false;
}
}
}
xhr.onprogress = (event) => {
if (event.lengthComputable) {
@ -97,6 +98,7 @@
}
xhr.onerror = () => {
console.error('网络或服务器错误');
downloading = false;
}
xhr.send();