Repository navigation
Expand file tree
/
Copy pathLesson5_Stopwatch.html
More file actions
109 lines (94 loc) · 2.63 KB
/
Copy pathLesson5_Stopwatch.html
File metadata and controls
109 lines (94 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<head>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab" rel="stylesheet">
</head>
<style>
#root{
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
color: white;
background: url('images/darkwood.jpg') no-repeat center;
background-size: cover;
font-family: 'Roboto Slab', serif;
}
.title{
font-size: calc(35px + 1vw);
}
.mainDiv{
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: center;
height: 50vh;
}
button{
border-radius: 50px;
background: transparent;
color: white;
padding: 20px;
min-width: 100px;
font-family: 'Roboto Slab', serif;
font-size: 30px
}
label{
font-size: 35px;
}
button:hover{
cursor: pointer;
}
button:focus {
outline: none;
}
</style>
<body id="root"></body>
<script type="text/babel">
class StopWatch extends React.Component{
state = {lapse: 0, running: false};
handleRunClick = () => {
this.setState(state => {
if(state.running){
clearInterval(this.timer);
}else{
const startTime = Date.now() - this.state.lapse;
this.timer = setInterval(() => {
this.setState({
lapse: Date.now() - startTime,
})
})
}
return {running: !state.running};
})
}
handleClearClick = () => {
storedValues.push("1");
clearInterval(this.timer);
this.setState({lapse: 0, running: false})
}
componentWillUnmounct(){
clearInterval(this.timer);
}
render(){
const {lapse, running} = this.state;
return (
<div className="mainDiv">
<div className='title'>React Stopwatch</div>
<label>{lapse}ms</label>
<button onClick={this.handleRunClick}>{running ? '||' : '>'}</button>
<button onClick={this.handleClearClick}>Clear</button>
</div>
)
}
}
let storedValues = [];
const element = (
<StopWatch />
)
ReactDOM.render(
element,
document.getElementById('root')
)
</script>