我有一个代码可以获取位置数据(如果找到它)并且每5秒保持一次刷新效果很好(仅当找到位置时)。但是,如果在提取时遇到问题,我会发出错误消息"无法获取.."但是如果这个消息出现,jQuery似乎永远不会再次刷新..有关如何解决这个问题的任何想法?感谢任何意见或建议。
$(document).ready(function() {
var lookup_time = 5000;
var animation_time = 1000;
var elapsed_realtime = null;
var ajax_timeout = 20000; // 30 Seconds before ajax fails
$(document.body).fadeIn(animation_time);
(function get_location() {
if(elapsed_realtime != null) {
clearInterval(elapsed_realtime);
}
$('#map_view,#elapsed_timer').fadeOut(0);
$('#locate_me').fadeOut(0).html('').fadeIn(animation_time); /*Fetching*/
$('#fetch_me').fadeIn(1000);
$('#cannot_me').fadeOut(0);
$.ajax({
url: '/locate/locate.php',
timeout: ajax_timeout,
success: function(data) {
$('#locate_me').fadeOut(0);
if(data['location'] == undefined) {
$('#locate_me').fadeOut(0)
.html('') /*Cannot fetch data at this time, try again later.*/
.fadeIn(animation_time);
$('#cannot_me').fadeIn(1000);
$('#fetch_me').fadeOut(0);
return; /////////////// It never Re-fetches after this return//////////////////
}
$('#locate_me').fadeOut(0)
.html(data['location'])
.fadeIn(animation_time);
$('#fetch_me').fadeOut(0);
$('#map_view').prop('href',data['map']);
$('#map_view').fadeIn(animation_time);
$('#elapsed_timer').fadeIn(3000);
elapsed_realtime = setInterval(function() { get_elapsed(data['time'])}, 500);
//setTimeout(clearInterval(elapsed_realtime),lookup_time);
setTimeout(get_location,lookup_time);
}
});
}());
});