Reputation: 1119
suppose the response is coming from the server when i hit the GET request as:
[
{
id: 1
status: "Pending",
startedAt: "10:30",
endsAt: "12:30"
},
{
id: 2
status: "Pending",
startedAt: "11:30",
endsAt: "1:30"
},
....
]
now i want to change this status field after every 1 hour to Started and then Finished
how can i do this using node.js and what should be the correct implementation for doing this.
Upvotes: 1
Views: 2246
Reputation: 4457
You can simply use setInterval for performing the operation after a specific interval.
var yourData = [
{
id: 1
status: "Pending",
startedAt: "10:30",
endsAt: "12:30"
},
{
id: 2
status: "Pending",
startedAt: "11:30",
endsAt: "1:30"
},
....
]
async function updateData(){
//Call your API and update the data
yourData = await yourAPICall()
}
setInterval(updateData, 60 * 60 * 1000)
Upvotes: 1
Reputation: 7949
You can use the cron job
.
Step 1) npm i crontab --save
Step 2) npm i node-cron --save
Now in your index.js
or inide your main
js file of node . Wirte the following command .
const cron = require('node-cron');
const crontab = require('node-crontab');
crontab.scheduleJob("0 * * * * " , function(){
//do something cool
},{
schedule: true,
timezone: "Asia/kolkata"
});
The above function will run at every 1 hour and you can do your code inside that .
For more information , Go through the official Document
Upvotes: 2