react实现简单倒计时
2024-09-03 00:06:31
今天遇到一个简单的小功能,看网上的一些方法感觉不太适合,所以就手敲了一个,直接上代码!!!
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;
最新文章
- poj 2352 Stars 数星星 详解
- fedora 23如何实现 让root用户自动登录?
- spring4 mvc 出错
- kail-linux my need
- [原]编译Android源码过程中遇到的问题
- vector容器使用总结 .xml
- spring测试父类,使用junit-4.4.jar,spring-test.jar
- Python基本程序结构
- 【转】Linux下(C/C++)使用system()函数一定要谨慎
- Sublime Text3注册码
- 【转】Java 类的生命周期详解
- jbpm4.4 spring整合
- Android Java混淆(ProGuard)
- mysql 忘记root密码,重置密码,及重置密码后权限问题不能登录的解决方案
- 转载 线程池 异步I/O线程 <;第三篇>;
- 用DBContext (EF) 实现通用增删改查的REST方法
- 配置ssh的config文件-为每个ssh连接创建别名
- Apache24 + php5.6.31 +Sql server R2 环境搭建①
- Linux初学之vmware Workstation 网络连接三种模式
- set get方法诡异的空指针异常