Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Stopwatch, Timer } from 'react-native-stopwatch-timer'
|options|object|describes style of rendered timer/stopwatch|see example|
|getTime|function|get the formatted value on each tick|(time) => console.log(time)|
|getMsecs|function|get the number of msecs on each tick|(time) => console.log(time)|
|tickInterval|number|set the interval for the getTime and getMsecs functions to be executed|1ms


#### Stopwatch Options
Expand Down
6 changes: 5 additions & 1 deletion lib/stopwatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class StopWatch extends Component {
getTime: PropTypes.func,
startTime: PropTypes.number,
getMsecs: PropTypes.func,
tickInterval: PropTypes.number
}

constructor(props) {
Expand Down Expand Up @@ -68,6 +69,9 @@ class StopWatch extends Component {
}

start() {
// default to 1 millisecond
const tickInterval = this.props.tickInterval || 1

if (this.props.laps && this.state.elapsed) {
let lap = new Date() - this.state.stopTime;
this.setState({
Expand All @@ -81,7 +85,7 @@ class StopWatch extends Component {

this.interval = this.interval ? this.interval : setInterval(() => {
this.setState({elapsed: new Date() - this.state.startTime });
}, 1);
}, tickInterval);
}

stop() {
Expand Down
5 changes: 4 additions & 1 deletion lib/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Timer extends Component {
totalDuration: PropTypes.number,
getTime: PropTypes.func,
getMsecs: PropTypes.func,
tickInterval: PropTypes.number
}

constructor(props) {
Expand Down Expand Up @@ -62,6 +63,8 @@ class Timer extends Component {
start() {
const handleFinish = this.props.handleFinish ? this.props.handleFinish : () => alert("Timer Finished");
const endTime = new Date().getTime() + this.state.remainingTime;
// default to 1 millisecond
const tickInterval = this.props.tickInterval || 1
this.interval = setInterval(() => {
const remaining = endTime - new Date();
if(remaining <= 1000) {
Expand All @@ -71,7 +74,7 @@ class Timer extends Component {
return;
}
this.setState({remainingTime: remaining});
}, 1);
}, tickInterval);
}

stop() {
Expand Down