Cutdown.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div>
  3. <div class="deloy">倒计时:<span id="time">{{count}}</span></div>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. data() {
  9. return {
  10. count: 180,
  11. dealTime:0,
  12. timer: null
  13. };
  14. },
  15. props:{
  16. success:{
  17. type: Boolean,
  18. default :false
  19. },
  20. },
  21. created(){
  22. this.cutDown();
  23. },
  24. // mounted(){
  25. // this.cutDown();
  26. // },
  27. methods: {
  28. cutDown() {
  29. // const TIME_COUNT = 180;
  30. if (!this.timer) {
  31. // this.count = TIME_COUNT;
  32. this.timer = setInterval(() => {
  33. if (this.count > 0 ) {
  34. this.count--;
  35. if(this.success) {
  36. this.stopCutdown();
  37. }
  38. } else {
  39. clearInterval(this.timer);
  40. this.timer = null;
  41. }
  42. }, 1000);
  43. }
  44. },
  45. initTime(){ // 返回的时候初始化倒计时
  46. // this.clearTimer();
  47. clearInterval(this.timer);
  48. this.timer = null;
  49. this.count = 180;
  50. this.cutDown();
  51. },
  52. stopCutdown(){ //拼图成功要清理掉倒计时,然后向父组件传递用时
  53. this.dealTime = 180-this.count;
  54. this.$emit('overtime',this.dealTime);
  55. clearInterval(this.timer);
  56. this.timer = null;
  57. },
  58. clearTimer(){ //拼图成功后清除定时器
  59. clearInterval(this.timer);
  60. this.timer = null;
  61. }
  62. }
  63. };
  64. </script>
  65. <style scoped>
  66. .play-page .deloy {
  67. padding: 10px 20px;
  68. color: #fff;
  69. }
  70. </style>