如何檢查DOM中的值是否有變化?我正在調用Ajax函數從數據庫中提取一些數據。如果新值被推動,我想眨眼或做一些動畫那個特定的DIV。所以當「可用性」變化時,動畫只會一次。僅限於價值發生變化的DIV。檢查並比較新值,並根據該值進行動畫
這裏是我的代碼:
//function the get data from database
function getRealData() {
$.ajax({
url: 'test_api.php',
data: "",
dataType: 'json',
success: function (rows) {
var text = '';
for (var i in rows) {
var row = rows[i];
var availability = row[3];
var hostName = row[2];
var intranet = row[6];
var timeRespons = row[4];
//console.log(availability);
if (availability == 0){
var img = "img/tick.png";
}
if (availability == 1){
var img = "img/warning.png";
}
if (availability == 2){
var img = "img/alert.png";
}
text+= '<section class="square"><h3> ' + intranet + '</h3><img src='+img+' width="70" height="70" rel='+availability+' alt="warning"/><h4><img src="img/time_icon.png" width="20" height="20" alt="clock" class="clock" /> '+ timeRespons+'</h4>';
text += '</section>';
}
$("#journey").html(text);
}
});
}
//this refreshes data every 2seconds
setInterval(getRealData, 2000);
//call the function to display data
getRealData();
非常感謝您的幫助!
EDIT
輸出爲:
<div id="journey">
<div class="square>availability: 0 hostName: aaa intranet: ttt timeResponse:0.124</div>
<div class="square>availability: 0 hostName: qqq intranet: hhh timeResponse:0.064</div>
<div class="square>availability: 0 hostName: www intranet: nnn timeResponse:0.303</div>
<div class="square>availability: 0 hostName: rrr intranet: rrr timeResponse:0.019</div>
<div class="square>availability: 0 hostName: eee intranet: uuu timeResponse:0.025</div>
<div class="square>availability: 0 hostName: ggg intranet: ooo timeResponse:0.158</div>
</div>
是'#journey'應該動畫的div嗎? – user1
您是否每次通過數據庫調用都具有相同數量的部分?另外,是intranet字段唯一的名稱段? – James
是#journey是應該做動畫的div – medzi