收藏
回答

有人遇到过_this.setData is not a function报错吗

代码如下,报错地方代码为红色标注出来的部分:

sendMsg:function(){

    let that = this;

    let time=60;

    /*改变按钮可点击状态 */

    this.setData({

      isSend:false

    });

    /* 倒计时 */

    let timer=setInterval(()=>{

      if(time==0){

        clearInterval(timer);

        this.setData({

          isSend:true,

          sendMsg:'重新发送'

        });

      }else{

        time--;

        this.setData({

          isSend:false,

          sendMsg:time+'秒后重新发送'

        });

      }

    },1000);

    /* 发送短信 */

    wx.request({

      url:'https://XXXXXXXXXXX',

      method:'POST',

      data: {

        'phone': phone 

      },

      header: {

          'content-type': 'application/x-www-form-urlencoded'

      },

      success: function(res) {

        if(!!res.data.code || res.data.code==0){

          if(res.data.code!=1){

            wx.showModal({

              title: '提示',

              content: res.data.message,

            });

            clearInterval(timer);

            that.setData({

              isSend:true,

              sendMsg:'重新发送'

            });

          }

        }else{

          clearInterval(timer);

          that.setData({

            isSend:true,

            sendMsg:'重新发送'

          });

        }

      },

      fail:function(res){

        clearInterval(timer);

        that.setData({

          isSend:true,

          sendMsg:'重新发送'

        });

      }

    });

}

报错如下:


然后把报错部分的this也修改成用that替换,还是报错


回答关注问题邀请回答
收藏

9 个回答

  • 李孔亮
    李孔亮
    03-11

    大兄弟最后这个怎么解决的

    03-11
    有用
    回复
  • lhy
    lhy
    2017-05-04

    @YJFn 是的,这里定时器是用的es6的箭头函数

    2017-05-04
    有用
    回复
  • YJFn
    YJFn
    2017-05-04

    我用你的代码来测,this是可以的

     let timer=setInterval(()=>{

    如果改成

    let timer=setInterval(function(){

    用this不行,用that可以


    2017-05-04
    有用
    回复
  • YJFn
    YJFn
    2017-05-04

    在定时器里用this肯定是不行的,至于为什么用that也不行,目前不清楚

    2017-05-04
    有用
    回复
  • lhy
    lhy
    2017-05-04

    @离乄殇 而且开始就只在请求回来以后才用的that替换this,后面把定时器里面的this也用that替换以后还是不行

    2017-05-04
    有用
    回复
  • lhy
    lhy
    2017-05-04

    @离乄殇 定义了的。。。

    2017-05-04
    有用
    回复
  • 远道
    远道
    2017-05-04

    定义一个变量that=this

    2017-05-04
    有用
    回复
  • lhy
    lhy
    2017-05-04

    @Remember_BTW火羊x2~ 定义了的,

    2017-05-04
    有用
    回复
  • Remember_BTW火羊x2~
    Remember_BTW火羊x2~
    2017-05-04

    异步 定义一个变量存储this

    2017-05-04
    有用
    回复
登录 后发表内容