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 小时前
F033 vue+neo4j图书智能问答+知识图谱推荐系统 |知识图谱+neo4j+vue+flask+mysql实现代码
vue.js·flask·nlp·neo4j·智能问答·图书·1024程序员节
kaikai_sk3 小时前
达梦数据库sql笔记
1024程序员节
码以致用3 小时前
StarRocks笔记
数据库·starrocks·olap·1024程序员节
大G的笔记本3 小时前
JVM 分代收集算法(Generational GC) 的原理和执行流程
1024程序员节
这张生成的图像能检测吗3 小时前
(论文速读)开放词汇3D场景理解的掩蔽点-实体对比
人工智能·计算机视觉·图像生成·1024程序员节·开放词汇·3d重建
eguid_13 小时前
【开源项目分享】JNSM1.2.0,支持批量管理的jar包安装成Windows服务可视化工具,基于Java实现的支持批量管理已经安装服务的可视化工具
java·开源·jar·1024程序员节·windows服务·jar包安装成服务·exe安装成服务
何如千泷3 小时前
【论文阅读】Qwen2.5-VL Technical Report
论文阅读·大模型·多模态·1024程序员节
歪歪1003 小时前
在C#中详细介绍一下Visual Studio中如何使用数据可视化工具
开发语言·前端·c#·visual studio code·visual studio·1024程序员节
北城笑笑3 小时前
Server 15 ,VMware ESXi 实战指南:Ubuntu 20.04.6 版本虚拟机静态 IP 配置、分辨率固定及远程访问实践
linux·运维·ubuntu·1024程序员节