2012-12-15 60 views
1

我在一个div加载这样的:刷新只有改变

var auto_refresh = setInterval(
function() 
{ 
    $('#nowplaying').load('nowplaying.php').fadeIn("slow"); 
}, 10000); 

我想,如果有一个在页面的改变只加载。 我怎样才能做到“改变”?

这是我已经试过:

function() 
{ 
    var cachedDOM = $('div#nowplaying').html(); 
    $.get('nowplaying.php',function(data){ 
     if (cachedDOM != data) { 
     $('#nowplaying').load('nowplaying.php').fadeIn("slow"); 
     } 
    } 
    ) 
}, 3000); 

的问题是, “cachedDOM =数据!” 始终是真实的。

帕特里克

+0

是其加载nowplaying.php反复? – Adil

+0

描述“页面的变化”,应该在什么时候发生? –

+0

@Xeano我认为OP意味着DOM元素的变化。 – inhan

回答

0

所以你需要另一个页面让你知道它是否改变。

var auto_refresh = setInterval(function() { 
    $.get('check_change.php', function(data) { 
    if (data.changed) { 
     $('#nowplaying').load('nowplaying.php').fadeIn("slow"); 
    } 
    }); 
}, 10000); 
+0

我试过了,但是当我更改'check_change.php'内容时它不会加载。 – trogne

+0

新的编辑,请参阅 – trogne

1

我想你的意思是改变了body。你可以这样做:

var cachedDOM = $('body').html(); 
var auto_refresh = setInterval(
function() 
{ 
    if(cachedDOM != $('body').html()) 
     $('#nowplaying').load('nowplaying.php',function(){cachedDOM = $('body').html();}).fadeIn("slow"); 
}, 10000); 
+0

谢谢,但我不认为它会工作。我想检查'nowplaying.php'内部是否发生了变化。如果是,则加载它。 – trogne

+0

所以你应该看到xdazz的答案,这是最好的方式去http://stackoverflow.com/a/13892797/1414562 –

0

而不是使用load,您可以使用ajax请求并将ifModified-Flag设置为true。这只会在页面内容发生变化时给您一个定义的响应。服务器端可能需要更多设置(etag)才能使其工作。 此处链接到文档:jquery ajax call

+0

你能告诉我怎么用我的例子吗? – trogne

+0

我不认为我根据您的需求回答了这个问题,最好尝试实施xdazz的解决方案。我的解决方案比它的价值更麻烦。 – mindandmedia

0

谢谢xdazz。 这就是我所做的:

var auto_refresh = setInterval(
function() 
{ 
function getcursong(){ 
var result = null; 
var scriptUrl = "get_current_song.php"; 
$.ajax({ 
    url: scriptUrl, 
    type: 'get', 
    dataType: 'html', 
    async: false, 
    success: function(data) { 
     result = data; 
    } 
}); 
return result; 
}  
gcs = getcursong(); 
okas = $('#song').html(); 
if (gcs != okas){ 
    $('#nowplaying').load('nowplaying.php').fadeIn("slow"); 
} 
}, 10000); 
0

,这是为我工作:

$(document).ready(function() { 
    function loadData() { 
     $('#nowplaying').load('currentsong.php').fadeIn("slow"); 
    } 

    var result = null; 

    function checkdata() { 
     var scriptUrl = "currentsong.php"; 
     $.ajax({ 
      url: scriptUrl, 
      type: 'get', 
      dataType: 'html', 
      async: true, 
      cache: false, 
      success: function(data) { 
       if (result != data) { 
        result = data; 
        loadData(); 
       }; 
      } 
     }); 
    } 

    var auto_refresh = setInterval(function() { 
     checkdata(); 
    }, 4000); 
});