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
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 data
withstorage
<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>