在加载 .json 文件数据后组合 if 语句运行时出现问题

我的页面从 .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);



慕运维8079593
浏览 162回答 1
1回答

明月笑刀无情

在您的上一个代码中,您将whatever()函数包装在代码周围,并且您then是setInterval 尝试这个的一部分var previous = null;var current = null;$(document).ready(() => {&nbsp; function whatever() {&nbsp; var what = document.getElementById('confirmRef').innerHTML;&nbsp; &nbsp; &nbsp; &nbsp; //console.log (what);if (what == 'true' ) {&nbsp; &nbsp; &nbsp; &nbsp;console.log ("call button should activate");&nbsp; &nbsp; &nbsp; &nbsp;//console.log (what);} else if (what == 'false' ) {&nbsp; &nbsp; &nbsp; &nbsp; console.log ("false confirmation");} else {&nbsp; &nbsp; &nbsp; &nbsp; console.log ("confirm says something else!");&nbsp; }}&nbsp; setInterval(function() {&nbsp; &nbsp; &nbsp; &nbsp; $.getJSON('thenumber.json', function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var text = `The number = ${data.thenumber}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Confirmed = : ${data.confirmed}`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var pass = `${data.thenumber}`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var conf = `${data.confirmed}`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(".mypanel").html(text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(".passRef").html(pass);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(".confirmRef").html(conf);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current = JSON.stringify();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (previous && current && previous !== current) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('refresh');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; location.reload();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; previous = current;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .then(() => whatever());&nbsp; &nbsp; }, 2000);});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript