1. 程式人生 > >微信小程式倒計時重置爬坑

微信小程式倒計時重置爬坑

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 () {
  
  }
})