uniapp倒计时跳转

文章描述:

uniapps倒计时跳转到指定页面,用于到用户在没有登录或者登录超时进行提示跳转

script

export default{
	data(){
		return{
			// 倒计时秒数
			seconds:3,
			// 定时器的 Id
			timer: null
		}
	},
	onLoad() {
		this.delayNavigate()
	},
	methods:{
		// 延迟导航到 my 页面
		delayNavigate() {
		  // 把 data 中的秒数重置成 3 秒
		  this.seconds = 3
		  this.showTips(this.seconds)

		  this.timer = setInterval(() => {
			this.seconds--

			if (this.seconds <= 0) {
			  clearInterval(this.timer)
			  uni.switchTab({
				url: '/pages/my/my'
			  })
			  return
			}

			this.showTips(this.seconds)
		  }, 1000)
		},
		// 展示倒计时的提示消息
		showTips(n) {
		  // 调用 uni.showToast() 方法,展示提示消息
		  uni.showToast({
			// 不展示任何图标
			icon: 'none',
			// 提示的消息
			title: '' + n + ' 秒后自动跳转',
			// 为页面添加透明遮罩,防止点击穿透
			mask: true,
			// 1.5 秒后自动消失
			duration: 1500
		  })
		}
	}
}

 

 

发布时间:2022/06/20

发表评论