使用watch.JS观看

时间:2019-06-21 14:33:05

标签: javascript jquery

我是JS的新手。我正在使用jquery从json格式加载数据。我想使用watchJS在数据的特定值更改时发出警报或console.log。如何使watchJS监视“ data.today”并在值更改时(而不是在工作人员刷新时)执行某些操作?

require(['watch', 'jquery'], function(WatchJS, $) {
          var watch = WatchJS.watch;
          var unwatch = WatchJS.unwatch;
          var callWatchers = WatchJS.callWatchers;

           $(document).ready(function () { 
             (function worker() {

               $.ajax({
                 dataType: "json",
                 url: 'ajax/dataload.php',

                 success: function(data) {
                   const pending = ("€" + data.pending);
                   const today = ("$" + data.today);
                   const week = ("$" + data.week);
                   const allTime = ("$"+ data.allTime);
                   const goal = "250";
                   const percent = (data.today/300*100)+"%";
                   const percentWeek = (data.week/2100*100 + "%");
                   const percentQ = (data.allTime/10000*100 + "%");

                   document.getElementById("progress").style.width = (percent);
                   document.getElementById("progressWeek").style.width = (percentWeek);
                   document.getElementById("progressQ").style.width = (percentQ);

                   $("#today").html(today);
                   $("#week").html(week);
                   $("#allTime").html(allTime);

                 },
                 complete: function() {
                   // Schedule the next request when the current one's complete
                   setTimeout(worker, 30000);
                 }
               });
             })();
           });
           });

0 个答案:

没有答案