关于微信小程序定时器的问题

今天产品经理给了一个需要,音频试听90s或者180s,我想到了定时器,然后效果是实现了,但是有一个bug的问题,这个bug是我进去试听完90s之后,退出来之后一直是没有声音,找了半天以为是我代码逻辑的问题,后台我发现了自己犯了一个低级的问题,我把定时器写错了,把最后的清楚定时器那写错了,一直没有清楚,所以报错

timefunction:function(){
    var that = this;
    that.setData({
      isBuyCourse: 1,
      isPlay: false,
    })
    var courseId = wx.getStorageSync('courseId');
    wx.request({//请求课程详情数据pa
      url: app.globalData.linkStr + '/HappyHui/queryCourseDetailApplets.do',
      method: "GET",
      header: {
        'content-type': 'application/text'
      },
      data: {
        course_id: courseId,
        user_id: that.data.userId,
        share_user_id: that.data.shareUserId
      },
      success: function (res) {
        console.log(res.data);
        console.log(res.data.free_time);
        console.log(res.data.is_free);
        var is_free = res.data.is_free;
        var free_time = res.data.free_time
        if (is_free == 0) {
          that.setData({
            isBuyCourse: 1,
            isPlay: false,
          })
          var theTime = res.data.begin_time;
          var nowDate = that.getNowDate();
          //限制是不是免费的
          var feel = that.data.whichPays;
          if (wx.canIUse("getBackgroundAudioManager")) {
            var backgroundAudioManager = wx.getBackgroundAudioManager();
            // console.log(that.CompareDate(nowDate, theTime));
            console.log(res.data.course_tv_path)
            if (res.data.course_tv_path) {
              wx.playBackgroundAudio({
                dataUrl: res.data.course_tv_path,
                title: res.data.course_title,
                coverImgUrl: res.data.course_img
              });
              backgroundAudioManager.onTimeUpdate(function (callback) {
                that.timer(true);
              });
            }
          }
        } else if (is_free == 1) {
          var time = 0;
          var timeallmore = setInterval(function () {
            time++;
            that.setData({
              isBuyCourse: 1,  
            })
            //3.如果时间大于10则是暂停收听音频
            if (time >= 10) {
              that.setData({
                isPlay: true,
                butVip: true,
                isShowLive: true,
                isBuyCourse: 0
              })
              
              var theTime = that.data.courseDetailData.begin_time;
              var nowDate = that.getNowDate();
              //限制是不是免费的
              var feel = that.data.whichPays;
              if (wx.canIUse("getBackgroundAudioManager")) {
                var backgroundAudioManager = wx.getBackgroundAudioManager();
                // console.log(that.CompareDate(nowDate, theTime));
                if (that.CompareDate(nowDate, theTime) == true) {
                  if (that.data.courseDetailData.course_tv_path) {
                    // console.log(that.data.isPlay);
                    if (that.data.isPlay == true) {//暂停
                      that.setData({
                        isPlay: false,
                        isBuyCourse: 1,
                      });
                      wx.pauseBackgroundAudio();
                      backgroundAudioManager.onTimeUpdate(function (callback) {
                        that.timer(false);
                      });
                    } else if (that.data.isPlay == false) {//播放
                      that.setData({
                        isPlay: true,
                      });
                      wx.playBackgroundAudio({
                        dataUrl: that.data.courseDetailData.course_tv_path,
                        title: that.data.courseDetailData.course_title,
                        coverImgUrl: that.data.courseDetailData.course_img
                      });
                      backgroundAudioManager.onTimeUpdate(function (callback) {
                        that.timer(true);
                      });
                    }
                  }
                }
              }
              clearInterval(timeallmore);
            }
          }, 1000)
        }
      }
    })  
  },

在这里插入图片描述
时刻警告自己以后不要这用低级的错误了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章