class App extends React.Component {
state = {
seconds: moment().format('HHmmss'),
turned: true,
}
tick() {
this.setState(() => ({
seconds: moment().format('HHmmss'),
}));
if (this.state.seconds >= '233000' && this.state.seconds <= '234000') { //make turned false between 23:30 - 23:40
this.setState(() => ({
turned: false,
}));
}
}
componentDidMount() {
this.interval = setInterval(() => this.submitToToday(), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
submitToToday() {
if (this.state.seconds >= '234500' && this.state.seconds <= '235500' && this.state.turned === false) {
// HERE MongoDB update that I want to run
this.setState(() => ({
turned: true,
}));
}
}
In this code, I set to run the function "submitToToday()" once between 23:45 - 23:55.
This works fine when I am on the webpage but this would do nothing when I am not on the webpage. Is there any way that I can run the code even if I am not on the webpage?
Or, is it possible to make the hosting service just open the webpage? I am using DigitalOcean.
If it is a DB operation as the comment suggest, the best way to do this would be in a CRON job. For Meteor just select one of the packages for CRON jobs that you like the most on Atmosphere: https://atmospherejs.com/?q=cron After that you can call a Meteor method which is going to create a new entry in the CRON to run the given task at the given time.