關於微信小程序定時器的問題

今天產品經理給了一個需要,音頻試聽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)
        }
      }
    })  
  },

在這裏插入圖片描述
時刻警告自己以後不要這用低級的錯誤了

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