2 回答

TA贡献2036条经验 获得超8个赞
您可以使用 setInterval
包装你的Request.getwith 函数并每 10 秒调用一次。
const apiRequest = () => {
Request.get("https://api.thingspeak.com/channels/709694/feeds.json?api_key=MY_API_KEY&results=1",(error,response,body)=>{
if(error){
return console.dir(error);
}
data = JSON.parse(body);
});
}
setTimeout(apiRequest, 10000);
如果您没有使用的确切理由nodejs Request,
考虑客户端 api 请求 with axios,并state更改 withreact也是一个不错的选择。

TA贡献1847条经验 获得超11个赞
以下是问题的解决方法。感谢@koo
const apiRequest = () => {
const url = 'https://api.thingspeak.com/channels/709694/feeds.json?api_key=MY_API_KEY&results=1';
$.ajax({url:url,success:function(result){
let field1 = result.feeds[0].field1;
$(".div1 p").text(field1);
}});
}
setInterval(apiRequest, 10000);
添加回答
举报