我找不到合适的方法来实现这一目标:
我有这个脚本
<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>
如果 JSON 上的 dateTime
已更改,则应每 3 秒检查一次。
问题是我无法进一步迈出第一步。我的意思是,当页面加载时,oldMessage 为空,因此 if 条件不满足。如果我可以“跳过”第一次迭代,那么一切都会顺利......
请您参考如下方法:
var oldMessage = false;
//...
if (oldMessage && oldMessage !== currentMessage) {
//...