Vue introduces a timer implementation code for answering questions

WBOY
Release: 2023-05-18 09:29:37
Original
773 people have browsed it

In recent years, technology in the front-end field has developed rapidly, and Vue.js, as a popular front-end framework, has been widely used and recognized. In actual development, it is often necessary to use timers to implement certain functions, such as timers for answering questions. Next, let’s introduce the implementation code of a timer for answering questions in Vue.js.

First of all, we need to use the timer component Vue Timer provided by Vue.js to implement the timer for the answer time. Vue Timer is a lightweight, simple and easy-to-use timer component that can easily implement various timer functions.

The following is the basic usage of Vue Timer:

1. Install Vue Timer

npm install --save vue-timer
Copy after login

2. Introduce Vue Timer into the Vue project

import Vue from 'vue'
import VueTimer from 'vue-timer'

Vue.use(VueTimer)
Copy after login

3. Use the Vue Timer component to implement the timer

<vue-timer 
    :time="time" 
    :autostart="false"
    @start="onStart"
    @pause="onPause"
    @resume="onResume"
    @stop="onStop">
    <div>{{ time | formatTime }}</div>
</vue-timer>
Copy after login

Among them, time represents the initial value of the timer, autostart represents whether to automatically start the timer, @start, @pause, @resume, and @stop represent the start and pause of the timer respectively. , events triggered when resuming and stopping. Finally, time is formatted into the specified format through the pipe character (|).

Next, we will learn more about the application of Vue Timer through the implementation code of a timer for answering questions.

<template>
  <div class="answerTime">
    <vue-timer 
        :time="time" 
        :autostart="autoStart" 
        @start="onStart"
        @pause="onPause"
        @resume="onResume"
        @stop="onStop">
        <div class="time">{{ time | formatTime }}</div>
    </vue-timer>
  </div>
</template>

<script>
import Vue from 'vue'
import VueTimer from 'vue-timer'
Vue.use(VueTimer)

export default {
  data() {
    return {
      time: 60 * 10, //初始时间为10分钟
      autoStart: true, //自动启动
      isPaused: false, //是否暂停
      remainingTime: 0 //剩余时间
    }
  },
  methods: {
    onStart() {
      console.log('计时器已启动')
    },
    onStop() {
      console.log('计时器已停止')
    },
    onPause() {
      console.log('计时器已暂停')
      this.isPaused = true
    },
    onResume() {
      console.log('计时器已恢复')
      this.isPaused = false
    }
  },
  filters: {
    formatTime: function (value) {
      if (!value) return '00:00'
      let minute = parseInt(value / 60)
      let second = parseInt(value % 60)
      return `${minute < 10 ? '0' + minute : minute}:${second < 10 ? '0' + second : second}`
    }
  },
  watch: {
    remainingTime: function (value) {
      if (value <= 0) {
        this.onPause()
        alert('答题时间已结束')
      }
    }
  },
  created() {
    setInterval(() => {
      if (!this.isPaused) {
        this.remainingTime = this.time - this.$refs.timer.seconds
      }
    }, 1000)
  }
}
</script>

<style>
  .time {
    font-size: 24px;
    color: #f60;
    text-align: center;
    margin-top: 20px;
  }
</style>
Copy after login

Through the above code, we have implemented a timer for the answering time. When the timer expires, a prompt box will pop up to remind the user that the answering time has ended. In the timer component, we can monitor events such as start, pause, resume, and stop of the timer to achieve flexible operations.

In general, Vue Timer is a very convenient and practical timer component. It is also very simple to use in Vue.js and can help us quickly implement various timer functions. I hope this article can help you better use Vue.js and Vue Timer components.

The above is the detailed content of Vue introduces a timer implementation code for answering questions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!