0

I am trying to make a dynamic dashboard that refreshes every x seconds to deliver new data from a PHP file. I want this to issue an Alert whenever there is a change in the data delivered, not whenever the data is refreshed. I am using require.js, watch.js and jQuery. I can`t get it to issue alert on changes on #tfpromo or the variable refreshId. Is there a way to do this in jQuery, JS or Watch.js?

PHP file:

$mainbalance = "foo";
    echo $mainbalance;

Index.html:

<div id="tfpromo"></div>
<script>
  require(['watch', 'jquery'], function(WatchJS, $) {

    var watch = WatchJS.watch;
    var unwatch = WatchJS.unwatch;
    var callWatchers = WatchJS.callWatchers;
    $(document).ready(function () { 
        $("#tfpromo").load("php/dataload.php");
        var refreshId = setInterval(function load2(){
          $("#tfpromo").load("php/dataload.php");
        }, 4000);

        watch(refreshId, function(){
          alert("balance changed!");
        });
      });
    });

    </script>
1
  • watch.js is for detecting changes to JavaScript objects, not DOM elements. Commented May 20, 2019 at 22:11

1 Answer 1

1

watch.js is for monitoring the value of JavaScript objects. So put the balance amount in an object, and update the object in the .load() callback.

    var watch = WatchJS.watch;
    var unwatch = WatchJS.unwatch;
    var callWatchers = WatchJS.callWatchers;
    $(document).ready(function () { 
        var balanceObj = {balance: 0};
        $("#tfpromo").load("php/dataload.php", function() {
            balanceObj.balance = $(this).text();
            watch(balanceObj, "balance", function() {
                alert("Balance changed!");
            });
        });
        var refreshId = setInterval(function load2(){
          $("#tfpromo").load("php/dataload.php", function() {
            balanceObj.balance = $(this).text();
          });
        }, 4000);

        watch(refreshId, function(){
          alert("balance changed!");
        });
      });
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.