我正在进行一个项目,在该项目中,我需要根据给定间隔后的日期和时间显示用户的详细信息。我正在使用以下代码,但它不符合我的要求。
$(document).ready(function () {
$.ajax({
type: "Post",
url: "<?php echo site_url('registrationc/map')?>",
// data: "unit_id="+unitid,
data: { startDate: startdate, endDate: enddate, status: status },
success: function (data) {
marker(data);
},
});
refresh();
});
function refresh() {
setInterval(function () {
$.ajax({
type: "Post",
url: "<?php echo site_url('registrationc/map')?>",
// data: "unit_id="+unitid,
data: { startDate: startdate, endDate: enddate, status: status },
success: function (data) {
alert("success 2");
alert(data);
marker(data);
},
});
}, 30000);
}
function marker_map(data){
var locations=JSON.parse(data);
alert(data);
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: new google.maps.LatLng(20.5937,78.9629),//{ lat: 20.5937, lng: 78.9629 }
mapTypeId: google.maps.MapTypeId.ROADMAP//HYBRID //SATELLITE//TERRAIN
});
var marker, i;
//var contentString ='<div id="iw-container"><div class="iw-title" align="center"></div>';
for (i = 0; i < locations.length; i++)
{
var url="http://maps.google.com/mapfiles/ms/icons/";
if(locations[i]['status']=="green")
{
url+="green"+"-dot.png";
}
else if(locations[i]['status']=="yellow")
{
url+="yellow"+"-dot.png";
}
else if(locations[i]['status']=="orange")
{
url+="orange"+"-dot.png";
}
else
{
url+="red"+"-dot.png";
}
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i]['lat'], locations[i]['longi']),
icon:{url:url},
map: map});
var land=locations[i][1];
var contentString = '<div id="content">'+
'<div class="iw-title">Patients Details</div>'+
'<h5 id="firstHeading" class="firstHeading">Patients Details</h5>'+
'<div id="bodyContent">'+
'<p>'+
'<p>Attribution: Uluru'+
'</p>'+
'</div>'+
'</div>';
google.maps.event.addListener(marker, 'click', (function(marker, i) {
var content = '<div id="iw-container">' +
'<div class="iw-title">'+locations[i]['firstname']+'</div>' +
'<div class="iw-content">' +
'<div class="iw-subTitle">STATUS:'+locations[i]['status']+'</div>' +
'<div class="iw-subTitle">PERSONAL DETAILS</div>'+
'<h6>Age:'+locations[i]['age']+'</h6>'+
'<h6>Gender:'+locations[i]['gender']+'</h6>'+
'</div>' +
'<div class="iw-bottom-gradient"></div>' +
'</div>';
var infowindow = new google.maps.InfoWindow({content:content});
return function() {
//infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
//marker.setIcon('http://maps.google.com/mapfiles/ms/icons/blue-dot.png');
}
})(marker, i));
}
}
ajax函数在给定的时间间隔后被调用,但它不会从数据库中获取新数据。例如,如果在第一次调用期间数据库中有三个条目,而在给定的间隔之后,第二次调用即使有五个条目,也只获取三个条目。我如何更改代码以获得预期结果。
您传递给ajax调用的数据似乎存在问题。比如开始日期和结束日期。
请尝试以下示例:
function getStats() {
var today = new Date();
var time = today.getHours() + ":" + today.getMinutes();
var time1 = " 00:00";
var status = "all";
var today = new Date();
var dd = String(today.getDate()).padStart(2, "0");
var mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0!
var yyyy = today.getFullYear();
var startdate = yyyy + "-" + mm + "-" + dd + "" + time1;
var enddate = yyyy + "-" + mm + "-" + dd + " " + time;
return { startdate, enddate, status };
}
$(document).ready(function () {
let { startdate, enddate, status } = getStats();
$.ajax({
type: "Post",
url: "<?php echo site_url('registrationc/map')?>",
data: { startDate: startdate, endDate: enddate, status: status },
success: function (data) {
marker(data);
},
});
refresh();
function refresh() {
setInterval(function () {
let { enddate } = getStats();
$.ajax({
type: "Post",
url: "<?php echo site_url('registrationc/map')?>",
data: { startDate: startdate, endDate: enddate, status: status },
success: function (data) {
marker(data);
},
});
}, 30000);
}
});