From d3178e2ebb1496d63a283ec1c2d0f34ba5125de7 Mon Sep 17 00:00:00 2001 From: carlosalmonte04 Date: Fri, 20 Jul 2018 23:06:02 -0400 Subject: [PATCH] clear pulse interval when changing from visible to not visible --- index.js | 88 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/index.js b/index.js index 9089b4d..eb1868f 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ -import React, { Component } from 'react'; -import PropTypes from 'prop-types'; +import React, { Component } from "react"; +import PropTypes from "prop-types"; import { Animated, InteractionManager, @@ -10,7 +10,7 @@ import { TouchableHighlight, TouchableOpacity, View -} from 'react-native'; +} from "react-native"; class StatusBarAlert extends Component { constructor(props) { @@ -30,7 +30,7 @@ class StatusBarAlert extends Component { Animated.parallel([ Animated.timing(this.state.height, { toValue: - Platform.OS === 'ios' + Platform.OS === "ios" ? this.props.height + (this.props.statusbarHeight || STATUS_BAR_HEIGHT) : this.props.height, @@ -42,27 +42,31 @@ class StatusBarAlert extends Component { }) ]).start(); }); - } - // Pulse animation - this.timer = setInterval(() => { - if (this.props.pulse) { - if (Math.round(this.state.pulse._value) === 1) { - Animated.timing(this.state.pulse, { - toValue: 0, - duration: PULSE_DURATION - }).start(); - } else { - Animated.timing(this.state.pulse, { - toValue: 1, - duration: PULSE_DURATION - }).start(); + // Pulse animation + this.timer = setInterval(() => { + if (this.props.pulse) { + if (Math.round(this.state.pulse._value) === 1) { + Animated.timing(this.state.pulse, { + toValue: 0, + duration: PULSE_DURATION + }).start(); + } else { + Animated.timing(this.state.pulse, { + toValue: 1, + duration: PULSE_DURATION + }).start(); + } } + }, PULSE_DURATION); + } else { + if (this.timer) { + clearInterval(this.timer); } - }, PULSE_DURATION); + } } componentWillUnmount() { - clearInterval(this.timer); + this.handleClearInterval(); } componentWillReceiveProps(nextProps) { @@ -73,7 +77,7 @@ class StatusBarAlert extends Component { Animated.parallel([ Animated.timing(this.state.height, { toValue: - Platform.OS === 'ios' + Platform.OS === "ios" ? nextProps.height + (this.props.statusbarHeight || STATUS_BAR_HEIGHT) : nextProps.height, @@ -100,10 +104,18 @@ class StatusBarAlert extends Component { }) ]).start(); }); + + this.handleClearInterval(); } } } + handleClearInterval() { + if (this.timer) { + clearInterval(this.timer); + } + } + render() { const content = this.props.children || (