需求
當用戶停留超過15分鍾后,用戶提交訂單,提示用戶超時並重新加載頁面
代碼
data () { return { // 超時定時器 overTimer: null, // 是否超時 isOvertime: false, } }, created () { // 開啟定時器 this.overTimer = setTimeout(() => { this.isOvertime = true; }, 900000) }, destroyed () { // 銷毀定時器 clearTimeout(this.overTimer) }, methods: { submitOrder () { // 判定是否超時 if (this.isOvertime) { this.$message.error('訂單已過期,請重新下單'); window.location.reload() } } }
定時器
如果方法還未被執行,可以使用 clearTimeout() 來停止計時器。
如果平時直接用的話,不必清除定時器,clearTimeout是在沒執行之前清除定時器
// 設置 setTimeout(function, milliseconds); // 清除 clearTimeout(id_of_settimeout)
類似的:
vue 用戶停留頁面超過30分鍾未操作 強制退出到登錄頁面
https://www.cnblogs.com/fmixue/p/10268660.html