4

i made 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 to console.log("neu"); only if data from AJAX is different than the data before, thats why i created a div "storage" where i save the latest data which was not equal to the data before.

But it doesnt works, it logs always "neu", even if the data isnt different.

UPDATE:

My new Code:

 <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 doesnt work

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Apple Web
  • 55
  • 8
  • 1
    Try changing `if($('.storage').html() !== data){` to `if($('.storage').text() !== data){` – Scott Marcus Oct 18 '17 at 14:05
  • What is the value of `$('.storage').html()`? What is the value of `data`? *Are* they ever the same? Note that your code never seems to *modify* the value of `$('.storage').html()`. – David Oct 18 '17 at 14:09
  • 2
    `500` aren't 5 seconds, it's a half seconds. Add one 0 at the end. – klenium Oct 18 '17 at 14:13
  • @klenium Yes i know, i didnt change the comment. If the new data changes storage than data and storage are the same strings. – Apple Web Oct 18 '17 at 14:18
  • Come on man, take 1/2 a second to format your code before posting it. – Scott Marcus Oct 18 '17 at 14:21
  • `text` needs to be `text()`. Read my comment. – Scott Marcus Oct 18 '17 at 14:22
  • I'm pretty sure text vs html is not his problem, unless `data` contains some [special characters](https://stackoverflow.com/questions/4459992/differences-between-text-and-html-with-escaped-and-characters) – klenium Oct 18 '17 at 14:26
  • 1
    Why adding another element? why not using a variable or using `.result` element? – HTMHell Oct 18 '17 at 14:47

2 Answers2

2

Because you are putting blank before compare. Remove this line from first line,

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

Or keep string in one variable to compare

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

And then compare data with storage

<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>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
1

In your code

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

this line makes storage blank remove it or place it after the function

Nisal Edu
  • 7,237
  • 4
  • 28
  • 34