我正在尝试创建一个呈现滴答作响的时钟的反应组件。我使用 moment 和 moment-timezone 来表示不同的时区。我能够创建一个带有静态时钟的组件(不递增),但我无法创建一个滴答作响的时钟。代码如下:
import React from 'react';
import ReactDOM from 'react-dom';
import $ from 'jquery';
import moment from 'moment';
import 'moment-timezone';
export default class TimeClock extends React.Component {
constructor(props) {
super(props);
this.state = { time: moment().clone().tz(this.props.timezone).toLocaleString() };
this.displayTime = this.displayTime.bind(this);
}
displayTime(){
//let now = moment();
//let location = now.clone().tz(this.props.timezone);
this.setState({
time: moment().clone().tz(this.props.timezone).toLocaleString()
});
//return location.toLocaleString();
}
render(){
//let now = moment();
//let location = now.clone().tz(this.props.timezone);
//let timezone = this.props.timezone;
return (
<div>
<p>{this.props.timezone}</p>
<p>{setInterval(this.displayTime,1000)}</p>
</div>
);
} …Run Code Online (Sandbox Code Playgroud)