SHARE
TWEET

Untitled

a guest Mar 25th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3.  
  4. export default class MouseMovement extends React.Component {
  5.   constructor(props) {
  6.     super(props);
  7.     this.state = {
  8.       timer: undefined,
  9.     };
  10.     this.timeout = this.timeout.bind(this);
  11.     this.onMouseMove = this.onMouseMove.bind(this);
  12.   }
  13.  
  14.   componentWillMount() {
  15.     document.addEventListener('mousemove', this.onMouseMove);
  16.     const timer = setTimeout(this.timeout, 4000);
  17.     this.setState({ timer });
  18.   }
  19.  
  20.   componentWillUnmount() {
  21.     document.removeEventListener('mousemove', this.onMouseMove);
  22.     clearTimeout(this.state.timer);
  23.     this.setState({ timer: undefined });
  24.   }
  25.  
  26.   onMouseMove() {
  27.     clearTimeout(this.state.timer);
  28.     const timer = setTimeout(this.timeout, 4000);
  29.     this.setState({ timer });
  30.     this.props.onMoved(true);
  31.   }
  32.  
  33.   timeout() {
  34.     clearTimeout(this.state.timer);
  35.     this.props.onMoved(false);
  36.   }
  37.   render() {
  38.     return null;
  39.   }
  40. }
  41.  
  42. MouseMovement.propTypes = {
  43.   onMoved: PropTypes.func.isRequired,
  44. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top