vue3 confirm倒计时

我用的vue3,element-plus,但是vue2应该也一样。我要实现的功能是密码重置后,弹出5s后自动关闭弹窗以及5s内用户点击确定就退出登录。

javascript 复制代码
// 在vue组件的data里
data() {
    return {
       _logoutTimer: null // 定时器
    }
}
javascript 复制代码
// 在vue的methods里
saveResetPwd() {
      // 重置成功后弹出 5 秒倒计时确认框:可点击"确定"立即退出或倒计时结束自动退出
      this.$refs.resetPwdForm.validate(valid => {
        if (!valid) return;
        this.resetPwd.username = this.userInfo.username;
        const { confirmPassword, ...requestParams } = this.resetPwd; // 去掉 confirmPassword
        this.$store.dispatch('ResetPassword', { requestParams, id: this.userInfo.id })
          .then(() => {
            this.showUserInfo = false;
            let seconds = 5;
            // 清理旧倒计时防止重复
            if (this._logoutTimer) {
              clearInterval(this._logoutTimer);
              this._logoutTimer = null;
            }

            const doLogout = () => {
              this.$store.dispatch('LogOut').then(() => {
                this.$router.push({ path: '/login' });
              });
            };

            const confirmPromise = this.$confirm(
              `密码修改成功,系统将在${seconds}s后自动退出,请您重新登录!`,
              '提示',
              {
                type: 'warning',
                showCancelButton: false,
                showClose: false,
                closeOnClickModal: false,
                closeOnPressEscape: false,
                confirmButtonText: `确定(${seconds}s)`
              }
            );

            // 用户点击"确定"提前退出
            confirmPromise.then(() => {
              if (this._logoutTimer) {
                clearInterval(this._logoutTimer);
                this._logoutTimer = null;
              }
              doLogout();
            }).catch(() => {
              // 异常关闭兜底退出
              if (this._logoutTimer) {
                clearInterval(this._logoutTimer);
                this._logoutTimer = null;
              }
              doLogout();
            });

            // 倒计时:更新按钮文字与提示文本;结束时自动关闭弹窗并退出
            this._logoutTimer = setInterval(() => {
              seconds--;
              if (seconds <= 0) {
                clearInterval(this._logoutTimer);
                this._logoutTimer = null;
                try { ElMessageBox.close(); } catch (e) {}
                doLogout();
              } else {
                const btn = document.querySelector('.el-message-box__btns .el-button--primary');
                if (btn) btn.textContent = `确定(${seconds}s)`;
                const msgP = document.querySelector('.el-message-box__message p');
                if (msgP) msgP.textContent = `密码修改成功,系统将在${seconds}s后自动退出,请您重新登录!`;
              }
            }, 1000);
          });
      });
    }

有人可能疑问为什么confirm的弹窗,为什么要ElMessageBox.close();去关闭,因为this.confirm只要不点击按钮它就只返回一个promise对象,根本拿不到实例的,this.confirm不能直接控制关闭,而且this.confirm源码里面调用的就是MessageBox(如下图)

相关推荐
李剑一3 小时前
字节一面,考察的够全面的啊!面试官:请分阶段解释一下从输入URL到页面渲染整个链路中的关键环节和可观测点
前端
xChive3 小时前
前端请求取消:用 AbortController 从 fetch 到 axios
前端·vue.js·axios·fetch·abortcontroller
Python大数据分析@3 小时前
HTML会代替Markdown吗?为什么?
前端·html
一棵树73513 小时前
js总结介绍
前端·javascript·html
jiayong233 小时前
前端面试题库 - 工程化与性能优化篇
前端·面试·性能优化
暗冰ཏོ3 小时前
2026前端开发资源大全:工具、文档、框架、学习路线与实战指南
前端·前端开发工具·前端编程工具·前端资源·前端开发文档
踩着两条虫3 小时前
AI 低代码引擎可视化设计器交互机制实战
前端·vue.js·人工智能·低代码·架构
ZC跨境爬虫3 小时前
跟着 MDN 学CSS day_2:(连接样式表与选择器的实战艺术)
java·前端·css·ui·html·媒体
lichenyang4533 小时前
鸿蒙聊天 Demo 练习 01:发送消息、模拟 AI 回复与自动滚动
前端
放下华子我只抽RuiKe53 小时前
React 从入门到生产(三):副作用与数据获取
前端·javascript·深度学习·react.js·开源·ecmascript·集成学习