Skip to content

Instantly share code, notes, and snippets.

@srph
Created November 16, 2014 15:13
Show Gist options
  • Save srph/fcd27d3e87a483b26ee9 to your computer and use it in GitHub Desktop.
Save srph/fcd27d3e87a483b26ee9 to your computer and use it in GitHub Desktop.
[ReactJS] basic countdown mechanics (https://gist.github.com/srph/fcd27d3e87a483b26ee9)
var CountdownControl = React.createClass({
// Simplified for brevity
render: function() {
var control = this.props.status
? 'Pause'
: 'Resume';
return (
<span onClick={this._handleClick}>
{control} |
</span>
);
},
_handleClick: function() {
// Toggle interval (clear or set) based on this.props.status
}
});
var CountdownTimer = React.createClass({
// Simplified for brevity
_setInterval: function() {
/* this.interval = setInterval....
* call setTick as callback for setInterval
*/
},
_clearInterval: function() { /* this.interval = clearInterval.. */ },
_setTick: function() { /* run tick */ },
render: function() {
return (
<CountdownControl
status={this.interval}
pause={this._setInterval}
resume={this._clearInterval} />
{this.state.elapsed} seconds has elapsed
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment