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(如下图)

相关推荐
zhuà!20 小时前
腾讯地图TMap标记反显,新增标记
前端·javascript·vue.js
未知原色20 小时前
web worker使用总结(包含多个worker)
前端·javascript·react.js·架构·node.js
ttod_qzstudio20 小时前
CSS改变图片颜色方法介绍
前端·css
curdcv_po20 小时前
我接入了微信小说小程序官方阅读器
前端·微信小程序
程序员鱼皮20 小时前
什么是 RESTful API?凭什么能流行 20 多年?
前端·后端·程序员
www_stdio20 小时前
让大语言模型拥有“记忆”:多轮对话与 LangChain 实践指南
前端·langchain·llm
inferno20 小时前
JavaScript 基础
开发语言·前端·javascript
cindershade21 小时前
Intersection Observer 的实战方案
前端
青莲84321 小时前
Kotlin Flow 深度探索与实践指南——中部:实战与应用篇
android·前端
cindershade21 小时前
事件委托(Event Delegation)的原理
前端