Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAlin Alexandru
630 Pointswhy is clock showing me NaN?
var Stopwatch = React.createClass({
getInitialState: function(){
return{
running:false,
elapsedTimpe: 0,
previousTime: 0,
}
},
componentDidMount: function(){
this.interval = setInterval(this.onTick, 100);
},
componentWillUmount: function(){
clearInterval(this.interval);
},
onTick: function () {
if(this.state.running){
var now = Date.now();
this.setState({
previousTime: now,
elapsedTime: this.state.elapsedTime + (now - this.state.previousTime)
});
}
console.log('onThick')
},
onStart: function(){
this.setState({
running: true,
previousTime: Date.now()
});
},
onStop: function(){
this.setState({running: false});
},
onReset: function(){
},
render: function () {
var seconds = Math.floor(this.state.elapsedTime / 1000);
return (
<div className="stopwatch">
<h2>Stopwatch</h2>
<div className="stopwatch-time">{seconds}</div>
{this.state.running ? <button onClick={this.onStop}>Stop</button> : <button onClick={this.onStart}>Start</button>}
<button onClick={this.onReset}>Reset</button>
</div>
);
}
})
[MOD edited code block - Steve Hunter]
1 Answer
Steven Parker
231,271 PointsA math operation was probably performed on a bad value.
Generally, "NaN" (which stands for "Not a Number") is the result of performing math on something other than a valid numeric value, such as a string.
Check the operands of your math statements and I'll bet you find one that's something you were not expecting.