小程序倒计时页面跳转

通常我们打开 APP 时,都会倒计时 5s, 5 s过后自动跳转到相应的页面,进入页面同时,清除原来的页面,同时不带返回箭头。

先上效果图:

HTML:

<view class='container'>
  <view class="welcome">
    <image src="../../img/logo.png"></image>
    <view class='bottom'>
      <text class='title'>您生活的小助手</text>
      <button catchtap="goHome">Welcome</button>
    </view>
  </view>

  <text class='time'>{{time}}s</text>
</view>

JS:

通过 setInterval 对时间倒计时操作,同时判断 time 小于等于零时,清除计时器,同时跳转页面。

使用  wx.reLaunch 关闭所有页面,打开到应用内的某个页面。

/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady() {
    //5s后跳转
    this.data.Time = setInterval(() => {
      this.setData({
        time: --this.data.time
      })
      if (this.data.time <= 0) {
        clearInterval(this.data.Time)
        this.goHome()
      }
    }, 1000)
},
goHome() {
    clearInterval(this.data.Time)
    wx.reLaunch({
      url: '../index/index'
    })
},
data: {
    time: 3,
},

CSS:

.container {
  width:100%;
  height:100vh;
  background: #00AFBE;
}
.welcome {
    width: 100%;
    height:100%;
    display:flex;
    justify-content: space-around;
    flex-direction: column;
    align-items: center;
}
.title{
  color:#fff;
  margin-top:80rpx;
  font-weight:900
}
.welcome .bottom{
  justify-content: flex-end
}
.welcome image {
    width: 120px;
    height: 120px;
}

.welcome button {
    background: #00AFBE;
    color: #fff;
    width: 260rpx;
    height:80rpx;
    opacity: 0;
    font-size:32rpx;
    animation: op 1s infinite;
    line-height: 80rpx;
    border:1px solid #fff;
    margin:40rpx 0
    
}

@keyframes op {
    0% {
        opacity: 0;
    }

    30% {
        opacity: 0.3;
    }

    60% {
        opacity: 0.6;
    }

    90% {
        opacity: 0.9;
        color: #fff;
    }

    100% {
        opacity: 1;
    }
}
.time {
    height: 40rpx;
    color: #fff;
    font-size: 24rpx;
    position: absolute;
    bottom: 5px;
    border-radius: 5px;
    line-height: 40rpx;
    right: 60rpx;
    padding:0 20rpx;
    border:1rpx solid #fff
}

 

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
实现倒计时可以使用setInterval()函数来定时执行代码。在小程序中,我们可以将倒计时的时间保存在Page对象的data中,然后在onLoad()函数中启动定时器,每隔一秒钟更新data中的倒计时时间。在页面切换时,可以使用wx.navigateTo()和wx.redirectTo()函数来进行页面跳转,这些函数不会影响定时器的执行。下面是一个实现倒计时切换页面的示例代码: ``` // pages/index/index.js Page({ data: { countdown: 60, // 倒计时时间 }, onLoad: function () { // 启动定时器 this.timer = setInterval(() => { if (this.data.countdown > 0) { this.setData({ countdown: this.data.countdown - 1, }); } else { clearInterval(this.timer); wx.navigateTo({ url: '/pages/result/result', }); } }, 1000); }, onUnload: function () { // 页面卸载时清除定时器 clearInterval(this.timer); }, navigateToResult: function () { // 手动跳转到结果页面 clearInterval(this.timer); wx.navigateTo({ url: '/pages/result/result', }); }, redirectToResult: function () { // 手动跳转到结果页面 clearInterval(this.timer); wx.redirectTo({ url: '/pages/result/result', }); }, }); // pages/result/result.js Page({ // 结果页面代码 }); ``` 在上面的代码中,onLoad()函数启动了一个定时器,每隔一秒钟更新data中的countdown值,当countdown值为0时,清除定时器并跳转到结果页面。在页面卸载时,需要清除定时器以避免内存泄漏。可以使用wx.navigateTo()和wx.redirectTo()函数手动跳转到结果页面,这些函数不会影响定时器的执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值