├── .gitignore ├── LICENSE ├── README.md ├── examples ├── example1.html └── src │ └── example1.js ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Daniel Cousens 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED: use [react-timer-mixin](https://github.com/reactjs/react-timer-mixin) instead 2 | ### react-timers 3 | 4 | [![Version](http://img.shields.io/npm/v/react-timers.svg)](https://www.npmjs.org/package/react-timers) 5 | 6 | A [react](https://github.com/facebook/react) `setInterval`/`setTimeout` mixin for mere mortals. 7 | Any intervals that are set are automatically cleared based on the component life cycle. 8 | 9 | **WARNING:** If you are performing an async action, you should still check if the component is mounted. 10 | 11 | 12 | ## Example 13 | 14 | ``` javascript 15 | var Timers = require('react-timers') 16 | 17 | module.exports = React.createClass({ 18 | mixins: [Timers], 19 | 20 | componentDidMount: function () { 21 | var self = this 22 | 23 | this.setTimeout(function () { 24 | self.setState({ lastUpdated: new Date() }) 25 | }, 1000) 26 | 27 | this.setInterval(function () { 28 | self.setState({ lastUpdated: new Date() }) 29 | }, 1000) 30 | }, 31 | 32 | render: function () { 33 | // ... etc 34 | } 35 | }) 36 | ``` 37 | 38 | 39 | ## LICENSE [MIT](LICENSE) 40 | -------------------------------------------------------------------------------- /examples/example1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | 13 |
14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /examples/src/example1.js: -------------------------------------------------------------------------------- 1 | let React = require('react') 2 | let Timers = require('../../src/') 3 | 4 | let App = React.createClass({ 5 | mixins: [Timers], 6 | 7 | getInitialState () { 8 | return { 9 | text: new Date() 10 | } 11 | }, 12 | 13 | componentDidMount () { 14 | this.setInterval(() => { 15 | this.setState({ 16 | text: new Date() 17 | }) 18 | }, 1000) 19 | }, 20 | 21 | render () { 22 | return

{this.state.text.toString()}

23 | } 24 | }) 25 | 26 | React.render(React.createElement(App), document.getElementById('app')) 27 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var GLOBAL = global || window 2 | 3 | function clearTimers () { 4 | this.clearIntervals() 5 | this.clearTimeouts() 6 | } 7 | 8 | module.exports = { 9 | clearIntervals: function clearIntervals () { this.__rt_intervals.forEach(GLOBAL.clearInterval) }, 10 | clearTimeouts: function clearTimeouts () { this.__rt_timeouts.forEach(GLOBAL.clearTimeout) }, 11 | clearInterval: function clearInterval (id) { return GLOBAL.clearInterval(id) }, 12 | clearTimeout: function clearTimeout (id) { return GLOBAL.clearTimeout(id) }, 13 | clearTimers: clearTimers, 14 | 15 | componentWillMount: function componentWillMount () { 16 | this.__rt_intervals = [] 17 | this.__rt_timeouts = [] 18 | }, 19 | componentWillUnmount: clearTimers, 20 | 21 | setInterval: function setInterval (callback) { 22 | var id = GLOBAL.setInterval(callback.bind(this), [].slice.call(arguments, 1)) 23 | this.__rt_intervals.push(id) 24 | return id 25 | }, 26 | setTimeout: function setTimeout (callback) { 27 | var id = GLOBAL.setTimeout(callback.bind(this), [].slice.call(arguments, 1)) 28 | this.__rt_timeouts.push(id) 29 | return id 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-timers", 3 | "version": "5.0.3", 4 | "description": "A react setInterval/setTimeout mixin for mere mortals.", 5 | "main": "index.js", 6 | "files": [ 7 | "index.js" 8 | ], 9 | "scripts": { 10 | "standard": "standard", 11 | "test": "npm run standard" 12 | }, 13 | "dependencies": {}, 14 | "devDependencies": { 15 | "react": "^0.15.5", 16 | "standard": "*" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/dcousens/react-timers.git" 21 | }, 22 | "bugs": { 23 | "url": "https://github.com/dcousens/react-timers/issues" 24 | }, 25 | "homepage": "https://github.com/dcousens/react-timers", 26 | "keywords": [ 27 | "clearInterval", 28 | "componentDidMount", 29 | "countdown", 30 | "interval", 31 | "mixin", 32 | "react", 33 | "time", 34 | "timeout", 35 | "timers", 36 | "sane", 37 | "setInterval", 38 | "setTimeout" 39 | ], 40 | "author": "Daniel Cousens", 41 | "license": "MIT" 42 | } 43 | --------------------------------------------------------------------------------