Javascript console.log()仅当AJAX数据更改时
我做了这个:Javascript console.log()仅当AJAX数据更改时,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我做了这个: <script type="text/javascript"> $('.storage').html(""); setInterval(function(){ $.get('./playcommand.php', function(data) { if($('.storage').html() !== data){ $('.result').html(data);
<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>
仍然不起作用,因为您在比较之前输入了空白。从第一行中删除此行
$('.storage').html("");
或者将字符串保留在一个变量中进行比较
var storage = $('.storage').text();
$('.storage').html("");
然后将数据
与存储
<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>
变量存储=$('.storage').text().trim();;
$('.storage').html(“”);
setInterval(函数(){
$.get('./playcommand.php',函数(数据){
if(存储!=data.trim()){
$('.result').html(数据);
控制台日志(“neu”);
}
});
}, 500);
在您的代码中
$('.storage').html("");
此行使存储为空将其删除或放在函数后面尝试将
if($('.storage').html()!==data){
更改为if($('.storage').text()!==data){
的值是多少($('.storage').html()
?数据的值是多少?是否相同?请注意,您的代码似乎从未修改$('.storage').html()的值
500
不是5秒,而是半秒。在末尾添加一个0。@klenium是的,我知道,我没有更改注释。如果新数据更改存储,那么数据和存储是相同的字符串。为什么要添加另一个元素?为什么不使用变量或使用。result
元素?仍然是相同的问题:(完全共享这两个字符串,或者尝试将其中一个作为静态字符串,如if(storage===“mytext”)
如果它运行意味着需要使用修剪函数或数据是静态的incorrect@AppleWeb:你试过这个解决方案吗?
$('.storage').html("");