我的页面从 .json 文件加载数据并将其显示在 html 页面中。 还有一个 Location.reload 元素,它会不断查找更改的数据,并在 .json 文件已更改时对其进行更新。请参阅代码屏幕 1
var previous = null;
var current = null;
setInterval(function() {
$.getJSON('thenumber.json', function(data) {
var text = `The number = ${data.thenumber}<br>
Confirmed = : ${data.confirmed}`
var pass = `${data.thenumber}`
var conf = `${data.confirmed}`
$(".mypanel").html(text);
$(".passRef").html(pass);
$(".confirmRef").html(conf);
current = JSON.stringify();
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
});
}, 2000);
我还想添加一些 if 语句,这些语句应该只在 .json 数据填充到 html 页面后运行,我开始工作(不包括重新加载功能(类似于代码屏幕 2(由于某种原因它今晚不起作用,叹息!
$(document).ready(() => {
function whatever() {
var what = document.getElementById('confirmRef').innerHTML;
if (what == 'true' ) {
console.log ("call button should activate");
} else if (what == 'false' ) {
console.log ("false confirmation");
} else {
console.log ("confirm says something else!");
}
$.getJSON('thenumber.json', function(data) {
var text = `The number = ${data.thenumber}<br>
Confirmed = : ${data.confirmed}`
var pass = `${data.thenumber}`
var conf = `${data.confirmed}`
$(".mypanel").html(text);
$(".passRef").html(pass);
$(".confirmRef").html(conf);
})
.then(() => whatever());
};
});
问题是当我尝试组合所有 3 个元素(代码屏幕 3(时,我在某处搞砸了它,并希望帮助我展示代码应该如何解决问题的示例代码。
var previous = null;
var current = null;
$(document).ready(() => {
function whatever() {
var what = document.getElementById('confirmRef').innerHTML;
//console.log (what);
if (what == 'true' ) {
console.log ("call button should activate");
//console.log (what);
} else if (what == 'false' ) {
console.log ("false confirmation");
} else {
console.log ("confirm says something else!");
}
setInterval(function() {
$.getJSON('thenumber.json', function(data) {
var text = `The number = ${data.thenumber}<br>
Confirmed = : ${data.confirmed}`
var pass = `${data.thenumber}`
var conf = `${data.confirmed}`
$(".mypanel").html(text);
$(".passRef").html(pass);
$(".confirmRef").html(conf);
current = JSON.stringify();
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
});
}, 2000);
.then(() => whatever());
});
谢谢 马克
在上一个代码中,您将whatever()
函数包装在代码周围,并且您的then
是setInterval
的一部分 试试这个
var previous = null;
var current = null;
$(document).ready(() => {
function whatever() {
var what = document.getElementById('confirmRef').innerHTML;
//console.log (what);
if (what == 'true' ) {
console.log ("call button should activate");
//console.log (what);
} else if (what == 'false' ) {
console.log ("false confirmation");
} else {
console.log ("confirm says something else!");
}
}
setInterval(function() {
$.getJSON('thenumber.json', function(data) {
var text = `The number = ${data.thenumber}<br>
Confirmed = : ${data.confirmed}`
var pass = `${data.thenumber}`
var conf = `${data.confirmed}`
$(".mypanel").html(text);
$(".passRef").html(pass);
$(".confirmRef").html(conf);
current = JSON.stringify();
if (previous && current && previous !== current) {
console.log('refresh');
location.reload();
}
previous = current;
})
.then(() => whatever());
}, 2000);
});