我在使用以下 javascript 时遇到问题。我正在使用 jQuery 框架。
此代码第一次运行时可以正确显示日期、温度和湿度,但第二次运行时出现此错误消息;
未捕获的语法错误:意外的标识符
这是代码;
var gettingTemperature = $.post("/get_temperature.php");
setInterval(gettingTemperature, 5000);
gettingTemperature.then(function (data) {
$('#date').text(data.date);
$('#temperature').html(data.temperature + "℃");
$('#humidity').text(data.humidity + "%");
}, function () {
console.log('Unable to access temperature model');
}
);
最佳答案
gettingTemperature
不代表对 $.post() 的调用,而是调用 $.post() 的结果,它是 jqXHR 对象
setInterval 需要一个函数引用作为第一个参数,但您交出了 jqXHR 对象。这就是为什么你运行 $.post() 一次(第一行)并且 setInterval 失败
var gettingTemperature = function {
$.post("/get_temperature.php").then(function (data) {
$('#date').text(data.date);
$('#temperature').html(data.temperature + "℃");
$('#humidity').text(data.humidity + "%");
}, function () {
console.log('Unable to access temperature model');
});
};
gettingTemperature();
setInterval(gettingTemperature, 5000);
关于javascript - javascript promise 和 setInterval 有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20144819/