阅读 41

uni 微信小程序定时器页面不更新

uni 编写微信小程序,uni中写定时器在小程序端会出现,页面更新不及时的情况

今天在项目中遇到了这个问题,苦苦找了2个小时,

下面以我在倒计时定时器中遇到的问题为例,如何去解决

for (let index in t.goodsList) {
  let timeLeave = new Date(t.goodsList[index].endTime).getTime() - new Date().getTime()
  if (timeLeave <= 0) {
    t.goodsList[index].goodhidden = true
    clearInterval(t.cutdownsetIntervalList[index])
    t.cutdownsetIntervalList[index] = null
    return
  }
  t.cutdownsetIntervalList[index] = setInterval(function() {
  if (t.homeTagIndex !== 4) {
    clearInterval(t.cutdownsetIntervalList[index])
    t.cutdownsetIntervalList[index] = null
    return
  }
  t.cutdownTimeList[index] = t.timeCl(t.goodsList[index].startTime, t.goodsList[index].endTime)
  t.$forceUpdate();
// console.log(t.cutdownTimeList[index]) // 这里输出的数据都会更新

 

一开始以为是定时器的问题,输出数据后发现,数据是每一秒中打印一次没有问题,

经过分析应该是微信小程序没有及时更新页面渲染,导致几秒钟才变更一次

for (let index in t.goodsList) {
  let timeLeave = new Date(t.goodsList[index].endTime).getTime() - new Date().getTime()
  if (timeLeave <= 0) {
    t.goodsList[index].goodhidden = true
    clearInterval(t.cutdownsetIntervalList[index])
    t.cutdownsetIntervalList[index] = null
    return
  }
  t.cutdownsetIntervalList[index] = setInterval(function() {
  if (t.homeTagIndex !== 4) {
    clearInterval(t.cutdownsetIntervalList[index])
    t.cutdownsetIntervalList[index] = null
    return
  }
  t.cutdownTimeList[index] = t.timeCl(t.goodsList[index].startTime, t.goodsList[index].endTime)
t.$forceUpdate();
//这里让微信小程序强制更新数据就可以解决这个问题

// console.log(t.cutdownTimeList[index]) // 这里输出的数据都会更新

 

原文:https://www.cnblogs.com/timesz/p/14779077.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐