diff --git a/src/styles/components/toast.css b/src/styles/components/toast.css index e11d4bf..c73264f 100644 --- a/src/styles/components/toast.css +++ b/src/styles/components/toast.css @@ -74,9 +74,9 @@ padding: 12px 14px; } -/* 下载通知自带进度条和状态,不需要底部计时条 */ +/* 下载通知自带进度条和状态;隐藏计时条但保留动画,自动关闭依赖 animationend */ .Toastify .download-toast .Toastify__progress-bar--wrp { - display: none; + visibility: hidden; } /* 服务器没给文件大小时的不确定进度:一段色条来回滑动 */ diff --git a/src/utils/download.tsx b/src/utils/download.tsx index 4cf4b4d..2ae736d 100644 --- a/src/utils/download.tsx +++ b/src/utils/download.tsx @@ -63,12 +63,11 @@ export async function downloadSong(props: DownloadSongParams): Promise { const toastId = toast.loading(render('loading'), { icon: false, closeButton: false, className: 'download-toast' }); // 进度事件很密集,合并到下一帧再刷新通知 - let scheduled = false; + let scheduled: number | undefined; const refresh = () => { - if (scheduled) return; - scheduled = true; - requestAnimationFrame(() => { - scheduled = false; + if (scheduled !== undefined) return; + scheduled = requestAnimationFrame(() => { + scheduled = undefined; toast.update(toastId, { render: render('loading') }); }); }; @@ -98,12 +97,14 @@ export async function downloadSong(props: DownloadSongParams): Promise { const results = await Promise.all(FILES.map(fetchOne)); const finish = (status: DownloadStatus) => { + // 最后一帧进度不能在完成后把通知改回下载中、覆盖自动关闭设置。 + if (scheduled !== undefined) cancelAnimationFrame(scheduled); toast.update(toastId, { render: render(status), type: status === 'done' ? 'success' : 'error', isLoading: false, icon: false, - autoClose: status === 'done' ? 3000 : 5000, + autoClose: 5000, }); };