如何在频繁点击node.js路由的setInterval中进行轮询
在我的应用程序中,我需要每两秒钟请求一次路由。我一直在尝试使用setInterval执行此操作,因为它抛出错误,因为如何在频繁点击node.js路由的setInterval中进行轮询,node.js,polling,Node.js,Polling,在我的应用程序中,我需要每两秒钟请求一次路由。我一直在尝试使用setInterval执行此操作,因为它抛出错误,因为无法渲染未知响应对象。我需要一种能够频繁渲染路由的方法 这是每2秒需要渲染的路线: app.get('/output',inputController.getValues); 路由的回调: var getValues = function(req,res){ var msg = JSON.stringify({ date: new Date().toString(), });
无法渲染未知响应对象
。我需要一种能够频繁渲染路由的方法
这是每2秒需要渲染的路线:
app.get('/output',inputController.getValues);
路由的回调:
var getValues = function(req,res){
var msg = JSON.stringify({
date: new Date().toString(),
});
mqttClient.publish(topic, msg, function() {
influxClient.query('sliderValue')
.then((rows) => {
console.log(rows.results);
res.render('output',{page_title:"Output",data:rows.results[0].series[0].values});
}).catch(console.error);
});
}
exports.getValues = getValues;
听起来你在找 例如:
var intervalID = window.setInterval(myCallback, 500);
function myCallback() {
// Your code here
}
另一个例子:
setInterval(
function() {
// Run your function here
}, 2000);
我早就试过了。它总是向我抛出错误,因为
无法读取未定义的属性render
您上面的代码没有显示您正在运行setInterval
,所以我假设您没有将其连接到函数,然后每隔2秒运行该函数。@Candy检查第二个示例。我在函数中有一个res
对象,所以当我执行该操作时第一次正确渲染视图时,它是未知的res
。在.catch()块之前,运行另一个.then()并在其中放置一个setInterval。