微信小程序定时器没一直减只减一次?,求指点

点击按钮之后一直是59不减了

data:{
tiem:60
}
code:function () {

var that = this;
var tiem = that.data.tiem;
if (tiem == 0) {
  that.setData({
    tiem: 60,
    true_false: true,
  });
  return;
}
var time = setInterval(function () {
  console.log(tiem-1)
  that.setData({
    true_false: false,
    tiem: tiem -1
  });
}, 1000)

}
clipboard.png

阅读 3k
3 个回答
var that = this;
var tiem = that.data.tiem;
if (tiem == 0) {
  that.setData({
    tiem: 60,
    true_false: true,
  });
  return;
}
var time = setInterval(function () {
  console.log(that.data.tiem-1)
  that.setData({
    true_false: false,
    tiem: that.data.tiem -1
  });
}, 1000)

tiem得在循环当中再获取一下

number是基本类型。。。tiem没有引用this.data.tiem

var time = setInterval(function () {
  console.log(this.data.tiem - 1)
  that.setData({
    true_false: false,
    tiem: this.data.tiem - 1
  });
}, 1000)
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题