{"id":8478,"date":"2021-05-26T23:50:55","date_gmt":"2021-05-26T15:50:55","guid":{"rendered":"https:\/\/sdeno.com\/?p=8478"},"modified":"2021-05-26T23:51:20","modified_gmt":"2021-05-26T15:51:20","slug":"vue%e5%80%92%e8%ae%a1%e6%97%b6","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=8478","title":{"rendered":"vue\u5012\u8ba1\u65f6"},"content":{"rendered":"<p>\u6587\u4ef6\u540d\uff1adownTime.vue<\/p>\n<pre>&lt;template&gt;\r\n  &lt;div class=\"countDownClass\"&gt;\r\n&lt;!--    &lt;span&gt;{{countDownText}}&lt;\/span&gt;--&gt;\r\n    &lt;img style=\"height: 5vw\" src=\"@\/assets\/imgs\/icon\/time_icon.png\" \/&gt;\r\n    &lt;span&gt;\r\n      &lt;span&gt;{{hhssmm.hou?hhssmm.hou:0}}&lt;\/span&gt;\r\n      &lt;span&gt;\u65f6&lt;\/span&gt;\r\n      &lt;span&gt;{{hhssmm.min?hhssmm.min:0}}&lt;\/span&gt;\r\n      &lt;span&gt;\u5206&lt;\/span&gt;\r\n      &lt;span&gt;{{hhssmm.sec?hhssmm.sec:0}}\u79d2&lt;\/span&gt;\r\n    &lt;\/span&gt;\r\n\r\n  &lt;\/div&gt;\r\n&lt;\/template&gt;\r\n\r\n&lt;script&gt;\r\n\r\nimport moment from 'moment'\r\n\r\nexport default {\r\n  name: \"CountDown\",\r\n  props: {\r\n    startTime: {\r\n      type: [Number, String],\r\n      required: true,\r\n    },\r\n    endTime: {\r\n      type: [Number, String],\r\n      required: true,\r\n    }\r\n  },\r\n  data() {\r\n    return {\r\n      countDownText: '',\r\n      hhssmm:{},\r\n    }\r\n  },\r\n  computed: {\r\n  },\r\n  async created() {\r\n    this.updateState();\r\n    this.timeInterval=setInterval(()=&gt;{\r\n      this.updateState()\r\n    },1000)\r\n  },\r\n  updated(){\r\n    if(this.end||this.done){\r\n      clearInterval(this.timeInterval)\r\n    }\r\n  },\r\n  methods: {\r\n\r\n    \/**\r\n     * \u65f6\u95f4\u5c0f\u4e8e10\u8865\u96f6\r\n     * *\/\r\n    timeFormat(param) {\r\n      return param &lt; 10 ? \"0\" + param : param;\r\n    },\r\n\r\n    \/**\r\n     * \u65f6\u95f4\u683c\u5f0f\u5316HHmmss\r\n     * *\/\r\n    formateHHmmss(time){\r\n      let day = parseInt(time \/1000 \/ (60 * 60 * 24));\r\n      let hou = parseInt((time  \/1000 % (60 * 60 * 24)) \/ 3600) + day * 24;\r\n      let min = parseInt(((time \/1000  % (60 * 60 * 24)) % 3600) \/ 60);\r\n      let sec = parseInt(((time \/1000 % (60 * 60 * 24)) % 3600) % 60);\r\n      let HHmmss = {\r\n        hou: this.timeFormat(hou),\r\n        min: this.timeFormat(min),\r\n        sec: this.timeFormat(sec)\r\n      };\r\n      return HHmmss;\r\n    },\r\n\r\n    \/**\r\n     * \u66f4\u65b0\u5012\u8ba1\u65f6\u72b6\u6001\r\n     *\/\r\n    updateState() {\r\n      console.log(this.startTime);\r\n\r\n      let starTime = new Date(moment(this.startTime).format(\"YYYY\/MM\/DD HH:mm:ss\")).getTime();\r\n      console.log(moment(this.startTime).format(\"YYYY\/MM\/DD HH:mm:ss\"))\r\n      let endTime = new Date(moment(this.endTime).format(\"YYYY\/MM\/DD HH:mm:ss\")).getTime();\r\n      var test = new Date(endTime);\r\n      \/\/console.log(test.getFullYear() + \"-\" + this.timeFormat(test.getMonth()) + \"-\" + this.timeFormat(test.getDate()) + \" \" + this.timeFormat(test.getHours()) + \":\" + this.timeFormat(test.getMinutes()) + \":\" + this.timeFormat(test.getSeconds()))\r\n      let nowTime = new Date().getTime();\r\n      let beginTime = starTime - nowTime;\r\n      let finishTime = endTime - nowTime;\r\n      let hhmmss = null;\r\n      if (beginTime &gt;= 0) {\r\n        let hou = this.formateHHmmss(beginTime).hou;\r\n        let min = this.formateHHmmss(beginTime).min;\r\n        let sec = this.formateHHmmss(beginTime).sec;\r\n        hhmmss = {\r\n          hou: hou,\r\n          min: min,\r\n          sec: sec\r\n        };\r\n        this.hhssmm = hhmmss;\r\n        this.start=true;\r\n        this.countDownText=\"\u79d2\u6740\u5df2\u5f00\u59cb\";\r\n      } else if (finishTime &gt;= 0) {\r\n        let hou = this.formateHHmmss(finishTime).hou;\r\n        let min = this.formateHHmmss(finishTime).min;\r\n        let sec = this.formateHHmmss(finishTime).sec;\r\n        hhmmss = {\r\n          hou: hou,\r\n          min: min,\r\n          sec: sec\r\n        };\r\n        this.hhssmm = hhmmss;\r\n        this.countDownText=`\u8ddd\u79bb\u6d3b\u52a8\u7ed3\u675f`;\r\n      } else {\r\n        hhmmss = {\r\n          hou: \"00\",\r\n          min: \"00\",\r\n          sec: \"00\"\r\n        };\r\n        this.end=true;\r\n        this.countDownText=\"\u6d3b\u52a8\u5df2\u7ed3\u675f\";\r\n      }\r\n    }\r\n  },\r\n  beforeDestroy() {\r\n    clearInterval(this.timeInterval)\r\n  }\r\n}\r\n&lt;\/script&gt;\r\n\r\n&lt;style scoped&gt;\r\n\/* \u79d2\u6740 *\/\r\n.countDownClass {\r\n  display: flex;\r\n  align-items: center;\r\n  flex-direction: row;\r\n  margin: 0 0 0.1rem 0;\r\n  color: #999999;\r\n  font-size: 4vw;\r\n}\r\n.countDownClass &gt; span:last-child {\r\n  margin-left: 0.1rem;\r\n}\r\n.countDownClass &gt; span:last-child span:nth-child(odd) {\r\n  color: #999999;\r\n  display: inline-block;\r\n  border-radius: 0.1rem;\r\n  padding: 0.05rem 0.05rem 0.05rem 0.05rem;\r\n}\r\n&lt;\/style&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u8c03\u7528\uff1a<\/p>\n<pre>import downTime from '@\/components\/downTime'\r\n\r\nexport default {\r\n   components: {downTime },\r\n}\r\n\r\n\r\n&lt;downTime :startTime=\"'2012-11-11 11:11:11'\" :endTime=\"'2022-11-11 11:11:11'\"&gt;&lt;\/downTime&gt;\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/chenqunjian\/article\/details\/102805369\" target=\"_blank\" rel=\"noopener\">https:\/\/blog.csdn.net\/chenqunjian\/article\/details\/102805369<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u4ef6\u540d\uff1adownTime.vue &lt;template&gt; &lt;div class=&#8221;countD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-8478","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8478"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/8478\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}