$('.storage').html(""); setInterval(function(){ $.get...">

Console.log () only if AJAX data is changed

I did this:

<script type="text/javascript">
    $('.storage').html("");
    setInterval(function(){
        $.get('./playcommand.php', function(data) {
            if($('.storage').html() !==  data){
              $('.result').html(data);
              console.log("neu");
            }
        });
    }, 500); // 5 seconds
</script>

My intention is console.log("neu");only if the data from AJAX is different from the data before, that’s why I created a div repository where I store the latest data that were not equal to the data before.

But it does not work, it always writes "neu", even if the data is not different.

UPDATE:

My new code is:

 <script type="text/javascript">
    var storage = $('.storage').text();
    setInterval(function(){
        $.get('./playcommand.php', function(data) {
            if($('.storage').text != data){
              $('.result').html(data);
              console.log("neu");
            }
        });
    }, 500); // 5 seconds
</script>

Still not working

+4
source share
2 answers

Because you put the blank before the comparison. Remove this line from the first line,

 $('.storage').html("");

Or save the string in one variable for comparison

 var storage = $('.storage').text();
 $('.storage').html("");

And then compare datawithstorage

<script type="text/javascript">
     var storage = $('.storage').text().trim();;
     $('.storage').html("");
     setInterval(function(){
        $.get('./playcommand.php', function(data) {
            if(storage != data.trim()){
              $('.result').html(data);
                console.log("neu");
            }
        });
    }, 500);
</script>
+2
source

In your code

 $('.storage').html("");

this line makes the memory empty by deleting it or placing it after the function

+1

Source: https://habr.com/ru/post/1687729/


All Articles