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

 import React, { Component } from 'react';

 class NoTimeContent extends Component {
constructor(props) {
super(props)
this.state = {
day: 0,
hour: 0,
minute: 0,
second: 0
}
}
render() {
return (
<NoTimeCon>
<h2>
<span>限时秒杀</span>
<span>{this.state.day}天 {this.state.hour}:{this.state.minute}:{this.state.second}</span>
</h2>
</NoTimeCon>
)
} componentDidMount() {
const end = Date.parse(new Date('2018-11-29 24:00'))
this.countFun(end);
} //卸载组件取消倒计时
componentWillUnmount(){
clearInterval(this.timer);
} countFun = (end) => { let now_time = Date.parse(new Date());
var remaining = end - now_time; this.timer = setInterval(() => {
//防止出现负数
if (remaining > 1000) {
remaining -= 1000;
let day = Math.floor((remaining / 1000 / 3600) / 24);
let hour = Math.floor((remaining / 1000 / 3600) % 24);
let minute = Math.floor((remaining / 1000 / 60) % 60);
let second = Math.floor(remaining / 1000 % 60); this.setState({
day:day,
hour:hour < 10 ? "0" + hour : hour,
minute:minute < 10 ? "0" + minute : minute,
second:second < 10 ? "0" + second : second
})
} else {
clearInterval(this.timer);
//倒计时结束时触发父组件的方法
//this.props.timeEnd();
}
}, 1000);
} }
export default NoTimeContent;

最新文章

  1. poj 2352 Stars 数星星 详解
  2. fedora 23如何实现 让root用户自动登录?
  3. spring4 mvc 出错
  4. kail-linux my need
  5. [原]编译Android源码过程中遇到的问题
  6. vector容器使用总结 .xml
  7. spring测试父类,使用junit-4.4.jar,spring-test.jar
  8. Python基本程序结构
  9. 【转】Linux下(C/C++)使用system()函数一定要谨慎
  10. Sublime Text3注册码
  11. 【转】Java 类的生命周期详解
  12. jbpm4.4 spring整合
  13. Android Java混淆(ProGuard)
  14. mysql 忘记root密码,重置密码,及重置密码后权限问题不能登录的解决方案
  15. 转载 线程池 异步I/O线程 &lt;第三篇&gt;
  16. 用DBContext (EF) 实现通用增删改查的REST方法
  17. 配置ssh的config文件-为每个ssh连接创建别名
  18. Apache24 + php5.6.31 +Sql server R2 环境搭建①
  19. Linux初学之vmware Workstation 网络连接三种模式
  20. set get方法诡异的空指针异常

热门文章

  1. Hive教程(1)
  2. (转载)Html解析工具Jsoup
  3. CentOS 7安装过程
  4. 用css3和canvas实现的蜂窝动画效果
  5. Object类方法简介二
  6. C#实现动态调用Windows DLL
  7. Windows 平台下 Go 语言的安装和环境变量设置
  8. 关于ShapeDrawable应用的一些介绍(中)之Gradient
  9. 国内物联网平台初探(七) ——Ablecloud物联网自助开发和大数据云平台
  10. LightOJ--1149--Factors and Multiples(二分图好题)