0
重新加载页面我无法找到一个合适的方式来实现这一目标:AJAX - 如果JSON已经改变
我有此脚本
<script type="text/javascript">
function updateSpots() {
$.ajax({
url : '/epark/api/spots/last',
dataType : 'text',
success : function(data) {
var json = $.parseJSON(data);
var currentMessage = json.dateTime;
var idPosto = json.idPosto;
console.log('current '+currentMessage);
console.log('old '+oldMessage);
if(currentMessage != oldMessage){
setTimeout(function(){location.reload();}, 5000);
$('#idPosto').toggle("higlight");
}
oldMessage = currentMessage;
}
});
}
var intervalId = 0;
intervalId = setInterval(updateSpots, 3000);
var oldMessage ="";
</script>
这应该检查每3秒,如果dateTime
对JSON改变。
问题是,我不能走得更远一步。我的意思是,当页面加载时,oldMessage
为空,所以if条件不满足。如果我能“跳”这第一次迭代,那么一切都会顺利......
你可以离开初始化为空字符串,但我认为'FALSE'更清晰 –
非常酷!谢谢 – besmart