countTimer.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div class="countdown-timer" v-if="!isCountdownFinished">
  3. <div>{{ remainingTime }}</div>
  4. <div>浏览得积分 +{{ score }}</div>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'CountdownTimer',
  10. props: {
  11. duration: {
  12. type: Number,
  13. require: true
  14. },
  15. onEnd: {
  16. type: Function,
  17. require: true
  18. },
  19. score: {
  20. type: Number,
  21. default: 100
  22. }
  23. },
  24. data() {
  25. return {
  26. count: 100,
  27. remainingTime: this.duration,
  28. isCountdownFinished: false // 倒计时是否结束
  29. }
  30. },
  31. mounted() {
  32. this.startTimer()
  33. },
  34. beforeUnmount() {
  35. this.stopTimer()
  36. },
  37. methods: {
  38. startTimer() {
  39. this.timer = setInterval(() => {
  40. if (this.remainingTime > 0) {
  41. this.remainingTime--
  42. // this.count = this.count - 6.67
  43. } else {
  44. this.stopTimer()
  45. this.isCountdownFinished = true // 设置倒计时结束状态
  46. this.onEnd()
  47. }
  48. }, 1000)
  49. },
  50. stopTimer() {
  51. clearInterval(this.timer)
  52. }
  53. }
  54. }
  55. </script>
  56. <style scoped>
  57. .countdown-timer {
  58. position: fixed;
  59. right: 20px;
  60. top: 30%;
  61. text-align: center;
  62. transform: translateY(-50%);
  63. background-color: rgba(0, 0, 0, 0.5);
  64. color: white;
  65. padding: 10px;
  66. border-radius: 5px;
  67. font-size: 16px;
  68. }
  69. </style>