-
Notifications
You must be signed in to change notification settings - Fork 0
/
code-timer.js
78 lines (62 loc) · 1.69 KB
/
code-timer.js
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
"use strict"
require("./node_modules/bootstrap/dist/css/bootstrap.min.css")
import React, { Component, PropTypes } from 'react'
import ReactDOM from 'react-dom'
export class TimerWrapper extends Component {
constructor (props) {
super(props)
this.state = {time:null, int:null}
this.startTimer = this.startTimer.bind(this)
}
startTimer (time) {
clearInterval(this.state.int)
var int = setInterval(() => {
let t1 = this.state.time - 1
if(t1 == 0) clearInterval(int)
this.setState({time: t1})
}, 1000)
return this.setState({time: time, int:int})
}
render () {
return (
<div className="row-fluid">
<h2>Timer</h2>
<div className="btn-group" role="group" >
<Button time="5" startTimer={this.startTimer}/>
<Button time="10" startTimer={this.startTimer}/>
<Button time="15" startTimer={this.startTimer}/>
</div>
<Timer time={this.state.time}/>
<audio id="end-of-time" src="flute_c_long_01.wav" preload="auto"></audio>
</div>
)
}
}
export class Timer extends Component {
constructor (props) {
super(props)
}
render () {
if(this.props.time == 0) {
document.getElementById('end-of-time').play()
}
if(this.props.time == null || this.props.time == 0) return <div/>
return <h1>Time left: {this.props.time}</h1>
}
}
export class Button extends Component {
constructor (props) {
super(props)
this.startTimer = this.startTimer.bind(this)
}
startTimer (e) {
return this.props.startTimer(this.props.time)
}
render () {
return <button type="button" className="btn-default btn" onClick={this.startTimer}>{this.props.time} seconds</button>
}
}
ReactDOM.render(
<TimerWrapper />,
document.getElementById('content')
)