猿问

使用 setInterval 的 Ajax 调用不会从数据库中获取新数据

我正在做一个项目,我需要在给定时间间隔后根据日期和时间显示用户详细信息。我正在使用下面的代码,但它不符合我的要求。


$(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});

ajax 函数在给定时间间隔后调用,但它不会从数据库中获取新数据。例如,如果在第一次调用期间数据库中有三个条目,而在给定时间间隔之后,第二次调用也只获取三个条目,即使有五个条目。如何我是否更改代码以获得预期结果。


哆啦的时光机
浏览 161回答 1
1回答

慕虎7371278

您传递给 ajax 调用的数据似乎存在问题。比如开始日期和结束日期。请尝试以下示例:function getStats() {&nbsp; var today = new Date();&nbsp; var time = today.getHours() + ":" + today.getMinutes();&nbsp; var time1 = " 00:00";&nbsp; var status = "all";&nbsp; var today = new Date();&nbsp; var dd = String(today.getDate()).padStart(2, "0");&nbsp; var mm = String(today.getMonth() + 1).padStart(2, "0"); //January is 0!&nbsp; var yyyy = today.getFullYear();&nbsp; var startdate = yyyy + "-" + mm + "-" + dd + "" + time1;&nbsp; var enddate = yyyy + "-" + mm + "-" + dd + " " + time;&nbsp; return { startdate, enddate, status };}$(document).ready(function () {&nbsp; let { startdate, enddate, status } = getStats();&nbsp; $.ajax({&nbsp; &nbsp; type: "Post",&nbsp; &nbsp; url: "<?php echo site_url('registrationc/map')?>",&nbsp; &nbsp; data: { startDate: startdate, endDate: enddate, status: status },&nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; marker(data);&nbsp; &nbsp; },&nbsp; });&nbsp; refresh();&nbsp; function refresh() {&nbsp; &nbsp; setInterval(function () {&nbsp; &nbsp; &nbsp; let { enddate } = getStats();&nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "Post",&nbsp; &nbsp; &nbsp; &nbsp; url: "<?php echo site_url('registrationc/map')?>",&nbsp; &nbsp; &nbsp; &nbsp; data: { startDate: startdate, endDate: enddate, status: status },&nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; marker(data);&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }, 30000);&nbsp; }});
随时随地看视频慕课网APP
我要回答