我有一個包含以下資料的資料庫條目:
{
_id:"fdjshbjds564564sfsdf",
shipmentCreationTime:"12:17 AM"
shipmentExpiryTime:"12:32 AM"
}
我想要做的是根據資料集中提供的創建和到期時間從前端(ReactJS)創建一個計時器。一旦計時器運行為零,我想執行一個 API 呼叫回后端。
功能如下:
exports.expireShipmentOffer = async (req,res) => {
const { shipmentOfferId } = req.body;
try {
const updatedOffer = await ShipmentOrder.findOneAndUpdate({_id: shipmentOfferId} , {$set:{status:'Expired'}} , {new: true})
const newNotification = await Notification({accountId: updatedOffer.accountId , notificationHeader: ` Shipment Order Expired ` , notificationMessage:`Your Shipment Request ${shipmentOfferId} has been expired due to no response from the carrier` , timeAndDate : moment().format('LLL'), actionPath:`/shipmentOffer/${updatedOffer._id}`})
await newNotification.save();
res.json({
status: 200,
message: updatedOffer
})
} catch (error) {
res.json({
status: 500,
message:error.message
})
}
}
我能做些什么來實作這一目標?
uj5u.com熱心網友回復:
您可以通過不同的方式實作這一點,其中之一是根據您的資料集注冊一個回呼并使用 setTimout() 函式,然后您可以在該函式中執行請求
uj5u.com熱心網友回復:
我不是很明白。
您要創建倒計時還是計時器?
如果您想創建一個 Countdown,請將值保存在一個狀態中,并在它為零時呼叫您的 API。
但是如果你想創建一個計時器,我建議你使用Promise docs
const tiemr = () => {
return new Promise(done => {
setTimeout(() => {
done(callYourAPI());
}, 5000); // after 5s
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/317746.html
標籤:javascript html 节点.js 反应 网络
