2012-12-15 59 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); 
});