你能告诉我为什么会收到这个错误吗?我正在尝试在React Native中进行日期倒计时,但是我无法让倒计时更新状态。提前谢谢。
以下是代码:
componentDidMount(){
var end = new Date(theDate);
console.log("end " + end);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
console.log("End is: " + end);
console.log("Now is: " + now);
var distance = end - now;
console.log("Distance is: " + distance)
if (distance < 0) {
clearInterval(timer);
console.log('EXPIRED!');
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
this.setState({
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
}.bind(this));
console.log("Remaining: " + days + ' days ' + hours + 'hrs '+ minutes + 'mins ' + seconds + ' secs')
}
timer = setInterval(showRemaining, 1000);
}
答案 0 :(得分:0)
只有一个地方可以拨打.bind
:
this.setState({
days: days,
hours: hours,
minutes: minutes,
seconds: seconds
}.bind(this));
对象没有.bind
方法,only functions do。不确定您在.bind
尝试实现的目标。只需删除它。