I'm trying to implement a component which performs a hh:mm:ss countdown for my project. first I used this library for calculating and rendering time for me. it worked perfect on any devices but iOS which return
NaN:NaN:NaN
. I assumed it's a problem with this module so I tried to implement my own countdown component. so I tried the code below:
import React from 'react';
import PropTypes from 'prop-types';
class Countdown extends React.Component {
constructor(props) {
super(props);
this.state = {
days: 0,
hours: 0,
min: 0,
sec: 0
};
}
componentDidMount() {
// update every second
let i = 0;
this.interval = setInterval(() => {
let date = this.calculateCountdown(this.props.date, ++i);
date ? this.setState(date) : this.stop();
}, 1000);
}
componentWillUnmount() {
this.stop();
}
calculateCountdown(endDate , count) {
let diff = (endDate - (count * 1000)) / 1000;
// clear countdown when date is reached
if (diff <= 0) return false;
const timeLeft = {
years: 0,
days: 0,
hours: 0,
min: 0,
sec: 0,
millisec: 0
};
// calculate time difference between now and expected date
if (diff >= (365.25 * 86400)) { // 365.25 * 24 * 60 * 60
timeLeft.years = Math.floor(diff / (365.25 * 86400));
diff -= timeLeft.years * 365.25 * 86400;
}
if (diff >= 86400) { // 24 * 60 * 60
timeLeft.days = Math.floor(diff / 86400);
diff -= timeLeft.days * 86400;
}
if (diff >= 3600) { // 60 * 60
timeLeft.hours = Math.floor(diff / 3600);
diff -= timeLeft.hours * 3600;
}
if (diff >= 60) {
timeLeft.min = Math.floor(diff / 60);
diff -= timeLeft.min * 60;
}
timeLeft.sec = Math.floor(diff);
return timeLeft;
}
stop() {
clearInterval(this.interval);
}
addLeadingZeros(value) {
value = String(value);
while (value.length < 2) {
value = '0' + value;
}
return value;
}
convertToPersianNumber = (time) => {
let persianDigits = '۰۱۲۳۴۵۶۷۸۹';
let persianMap = persianDigits.split('');
return time.replace(/\d/g, m => persianMap[parseInt(m)]);
};
render() {
const countDown = this.state;
return (
<div className="Countdown">
<span className="Countdown-col">
<span className="Countdown-col-element">
{countDown.hour}
{/*{this.convertToPersianNumber(this.addLeadingZeros(countDown.hours))}:*/}
</span>
</span>
<span className="Countdown-col">
<span className="Countdown-col-element">
{countDown.min}
{/*{this.convertToPersianNumber(this.addLeadingZeros(countDown.min))}:*/}
</span>
</span>
<span className="Countdown-col">
<span className="Countdown-col-element">
{countDown.sec}
{/*{this.convertToPersianNumber(this.addLeadingZeros(countDown.sec))}*/}
</span>
</span>
</div>
);
}
}
Countdown.propTypes = {
date: PropTypes.number.isRequired
};
Countdown.defaultProps = {
date: 0
};
export default Countdown;
in this component I pass my end time in mili seconds and calculate time by an interval , finally I convert it to persian strings and print it.
<div className={styles.bottomContainer}>
{/*<Moment>{remainTime}</Moment>*/}
<CountDown onComplete={() => this.setState({ timerEnd: true })}
date={remainTime}/>
</div>
I still have the same problem I had before: in iOS devices I get
NaN:00:00
what is my problem if you can help me? I have to add that I cleared converting and tried to printing just numbers and it didn't work;