我似乎无法全力以赴地更新页面上的特定div而无需刷新。当前,如果刷新页面,则如果上一次更新少于60秒,则会向数据库发出请求并获取清单。但是,例如,我想每5秒动态获取一次此信息,但不知道如何
homeController.js
//GET request loads game home page
app.get('/game/:gameid/home', function(req,res){
if(global.gameId == req.params.gameid){
updateInventory().then(function(){
Promise.all([getResourceInventory(), getResourceProduction(), getResourceLevel(), canUpgradeResourceField()]).then(function(data){ //Promise.all returns an array of all resolves from every function called
res.render('GameEngine/home/gameHome', {resourceInventory: data[0],
resourceProduction: data[1],
resourceLevel: data[2],
canUpgradeCrop: data[3][0],
canUpgradeLumber: data[3][1],
canUpgradeOre: data[3][2],
canUpgradeOil: data[3][3]});
});
});
} else{
res.send(500, "Not authorized to view this page.");
}
});
gameHome.ejs
<div class = "container container-fluid resourcesInventory">
<h3>Inventory </h3>
<li> Crop Inventory: <%= resourceInventory[0] %> </li>
<li> Lumber Inventory: <%= resourceInventory[1] %></li>
<li> Ore Inventory: <%= resourceInventory[2] %></li>
<li> Oil Inventory: <%= resourceInventory[3] %></li>
</div>
<div class = "container container-fluid resourcesProduction">
<h3>Production </h3>
<li> Crop Production: <%= resourceProduction[0] %> </li>
<li> Lumber Production: <%= resourceProduction[1] %></li>
<li> Ore Production: <%= resourceProduction[2] %></li>
<li> Oil Production: <%= resourceProduction[3] %></li>
</div>
<div class = "container container-fluid resourcesLevel">
<h3>Level</h3>
<li> Crop: Level <%= resourceLevel[0] %> </li>
<li> Lumber: Level <%= resourceLevel[1] %></li>
<li> Ore: Level <%= resourceLevel[2] %></li>
<li> Oil: Level <%= resourceLevel[3] %></li>
</div>