博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react实现简单倒计时
阅读量:4695 次
发布时间:2019-06-09

本文共 1782 字,大约阅读时间需要 5 分钟。

今天遇到一个简单的小功能,看网上的一些方法感觉不太适合,所以就手敲了一个,直接上代码!!!

1 import React, { Component } from 'react'; 2  3 class NoTimeContent extends Component { 4     constructor(props) { 5         super(props) 6         this.state = { 7             day: 0, 8             hour: 0, 9             minute: 0,10             second: 011         }12     }13     render() {14         return (15             
16

17 限时秒杀18 {
this.state.day}天 {
this.state.hour}:{
this.state.minute}:{
this.state.second}
19

20
21 )22 }23 24 componentDidMount() {25 const end = Date.parse(new Date('2018-11-29 24:00'))26 this.countFun(end);27 }28 29 //卸载组件取消倒计时30 componentWillUnmount(){31 clearInterval(this.timer);32 }33 34 countFun = (end) => {35 36 let now_time = Date.parse(new Date());37 var remaining = end - now_time;38 39 this.timer = setInterval(() => {40 //防止出现负数41 if (remaining > 1000) {42 remaining -= 1000;43 let day = Math.floor((remaining / 1000 / 3600) / 24);44 let hour = Math.floor((remaining / 1000 / 3600) % 24);45 let minute = Math.floor((remaining / 1000 / 60) % 60);46 let second = Math.floor(remaining / 1000 % 60);47 48 this.setState({49 day:day,50 hour:hour < 10 ? "0" + hour : hour,51 minute:minute < 10 ? "0" + minute : minute,52 second:second < 10 ? "0" + second : second53 })54 } else {55 clearInterval(this.timer);56 //倒计时结束时触发父组件的方法57 //this.props.timeEnd();58 }59 }, 1000);60 }61 62 }63 export default NoTimeContent;

 

转载于:https://www.cnblogs.com/luxiaot/p/10010149.html

你可能感兴趣的文章
windows live writer 2012 0x80070643
查看>>
tomcat 和MySQL的安装
查看>>
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
u-boot启动第一阶段
查看>>
MySQL批量SQL插入性能优化
查看>>
定义列属性:null,default,PK,auto_increment
查看>>
用户画像展示
查看>>
C#中StreamReader读取中文出现乱码
查看>>
使用BufferedReader的时候出现的问题
查看>>
批处理文件中的路径问题
查看>>
hibernate出现No row with the given identifier exists问题
查看>>
为什么wait()和notify()属于Object类
查看>>
配置NRPE的通讯
查看>>
shp系列(一)——利用C++进行shp文件的读(打开)与写(创建)开言
查看>>
匹配两个空格之间的字符。。。
查看>>
CSS 文字溢出 变成省略号 ...
查看>>
Spring事务
查看>>
java编程基础(三)流程控制语句
查看>>
让数据库跑的更快的7个MySQL优化建议
查看>>