我目前正在处理一个项目,无法真正了解如何刷新AJAX请求。目前,我的代码工作良好,我可以显示所有的标记,但我想刷新它每5秒,并收到一个新的!下面是我的脚本副本,我尝试使用setInterval,但没有成功jet!在这方面的帮助会很大的。再次感谢。
var gMapsLoaded = false;
window.gMapsCallback = function(){
gMapsLoaded = true;
$(window).trigger('gMapsLoaded');
}
window.loadGoogleMaps = function(){
if(gMapsLoaded) return window.gMapsCallback();
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src","http://maps.google.com/maps/api/js?sensor=false&callback=gMapsCallback");
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
}
$(document).ready(function(){
function initialize(){
var mapOptions = {
zoom: 18,
center: new google.maps.LatLng(51.5207239719, -0.182568696184),
mapTypeId: google.maps.MapTypeId.ROADMAP};
map = new google.maps.Map(document.getElementById('map_canvas'),mapOptions);
}
$(window).bind('gMapsLoaded', initialize);
window.loadGoogleMaps();
});
$(window).load(function update () {
$.ajax({
url: 'get-last.php',
success:function(data){
//Loop through each location.
$.each(data, function(){
//Plot the location as a marker
var pos = new google.maps.LatLng(this.latitude, this.longitude);
new google.maps.Marker({
position: pos,
map: map
});
});
} , complete: function() {
setInterval(update, 1000);
}
});
发布于 2016-06-13 23:47:56
您是否尝试过将update()
函数包装在setInterval()
中,而不是在完全回调时执行它?而且我看不出你在哪里定义了map
变量
$(window).load(function(){
update()
setInterval(function(){
update()
}, 5000)
})
function update(){
$.ajax({
url: 'get-last.php',
success:function(data){
//Loop through each location.
$.each(data, function(){
//Plot the location as a marker
var pos = new google.maps.LatLng(this.latitude, this.longitude);
new google.maps.Marker({
position: pos,
map: map //is this been set globally?
});
});
}
})
}
发布于 2016-06-13 23:23:54
为此使用setTimeout(function(){ alert("Hello"); }, 3000);
函数。它将执行该函数,并在特定时间后重新计算它。
https://stackoverflow.com/questions/37799147
复制相似问题