微信小程序倒计时重置爬坑

const constants = require('../../constants.js');
const request = require('../../utils/request.js');
var countdown = 0;
var countTimeout = null;
// pages/answer/answer.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totalScore: 0,
    rightCount: 0,  //答对题数
    questionId: 0,
    surplusNum: 0,  //剩余题数
    isCheck: false,
    characterArr: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    index: 1,  //第几题,
  },
  settime: function (that) {
    if (countdown == 0) {//倒计时结束
      if (that.data.isCheck == false && (that.data.surplusNum-1) != 0) {
        wx.showToast({
          title: '答题时间到',
          duration: 2000
        });
      }
      that.setData({
        surplusNum: that.data.surplusNum - 1,
        isCheck: true,
        questionId: that.data.question_arr[that.data.index - 1],
      })
      setTimeout(function () {
        if (that.data.surplusNum == 0) {
          that.endAnswer();
        } else {
          that.getNextQuestion();
        }
      }, 2000)
    } else {
      that.setData({
        last_time: countdown
      })
      countdown--;
      that.resetTimeout(function () {
        that.settime(that)
    }, 1000)
    }
  },
  resetTimeout : function(timeFunc,time){
    if (countTimeout != null){
      clearTimeout(countTimeout);
    }
    countTimeout = setTimeout(timeFunc,time);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.setData({
      starId: options.starId,
    });
    that.getNextQuestion();
  },
  //获取下一题
  getNextQuestion: function(){
    const that = this;
    request.post({
      url: '/api/answer/getQuestion',
      data: {
        questionId: that.data.questionId,
        starId: that.data.starId,
      },

      success: function (res) {
        console.log(res);
        const data = res.data.result
        if (res.data.status == 201){
          that.setData({
            question_arr: data.question_arr,
            question_count: data.question_count,
            question_options: data.question_options,
            question_time: data.question_time,
            question_title: data.question_title,
            score_set: data.score_set,
            isCheck: false,
            surplusNum: data.question_count,
            index: 1,
          });
        }else{
          that.setData({
            question_options: data.question_options,
            question_title: data.question_title,
            isCheck: false,
            index: that.data.index + 1,
          });
        }
        //加载完数据开始倒计时   
        countdown = data.question_time ? data.question_time : that.data.question_time;
        that.settime(that);
      },
      fail: function (e) {
        wx.showToast({
          title: '请求失败!',
          duration: 2000
        });
      },
    });
  },
  //点击选择答案
  checkIt: function (e) {
    var checkIdx = e.currentTarget.dataset.id;
    const that = this;
    that.setData({
      isCheck: true,
      surplusNum: that.data.surplusNum - 1,
      checkIdx, checkIdx,
      questionId: that.data.question_arr[that.data.index-1],
    });
    if (that.data.question_options[checkIdx].right_answer == 1){//答对加分
      const useTime = that.data.question_time - countdown;  //答题使用时间
      const scoreSet = that.data.score_set;
      for (let i = 0; i < scoreSet.length;i++){
        if (useTime > scoreSet[i].min_secend && useTime <= scoreSet[i].max_secend){
          var score = 0;
          if (that.data.surplusNum == 0) {//最后一题答对得分翻倍
            score = (that.data.totalScore + scoreSet[i].score) * 2;
          }else{
            score = that.data.totalScore + scoreSet[i].score
          }
          that.setData({
            rightCount: that.data.rightCount+1,
            totalScore: score,
          });
        }
      }
    }
    //清除旧的计时器
    that.resetTimeout(function () {
      console.log(that.data.surplusNum);
      if (that.data.surplusNum == 0){
        that.endAnswer();
      }else{
        that.getNextQuestion();
      }
    }, 1500)
  },
  //答题结束跳转页面
  endAnswer: function () {
    const that = this;
    const starId = that.data.starId;
    const totalScore = that.data.totalScore;
    const rightCount = that.data.rightCount;
    const questionCount = that.data.question_count;
    wx.navigateTo({
      url: '../time/time?starId=' + starId + '&totalScore=' + totalScore + '&rightCount=' + rightCount + '&questionCount=' + questionCount,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})

你可能感兴趣的:(微信小程序)