简体   繁体   English

将间隔设置为 AJAX 请求并将参数传递给 function

[英]Set Interval to AJAX request and pass parameters to function

I'm trying to do and Auto Update Chart, and for that I need to set an interval of about 5 seconds so my data can refresh, well I could do that for the data, but I have another request that gets the hour the data was sent, and I just don't know how to set an Interval to this AJAX request, here's what I got:我正在尝试自动更新图表,为此我需要设置大约 5 秒的间隔,以便我的数据可以刷新,我可以为数据执行此操作,但我有另一个请求获取数据的小时已发送,我只是不知道如何为此 AJAX 请求设置间隔,这就是我得到的:
This is what I have:这就是我所拥有的:

//I would have to add a 5 seconds interval so the hour(data) can update
 $.ajax({
      type: 'POST',
      url: 'loadchart.php',
      dataType: 'json',
      success: function timehour(data){
        var horario = []
        horario = data[5];

        hora(horario);
      }
    });

    function hora(tempo){
//This is data stuff, this is right
      //Live update das informacoes do sensor
      function loadData() {
        $.getJSON('loadchart.php', function(response) {
          myLineChart.data.datasets[0].data = response[0];
          myLineChart.data.datasets[1].data = response[1];
          myLineChart.data.datasets[2].data = response[2];
          myLineChart.data.datasets[3].data = response[3];
          myLineChart.data.labels = response[4];
          myLineChart.update();
        });
      }

      loadData();
      setInterval(loadData, 5000);

      var lbl = [];
      var ctx1 = document.getElementById('mychart1').getContext('2d');
//rest of the chart down here

New Attempt:新尝试:

function updateHour(res)
    {
      var horario = []
      horario = res[5];

      hora(horario);
    }

    function loadHour()
    {
      $.ajax({
        type: 'POST',
        url: 'loadchart.php',
        dataType: 'json',
        success: updateHour
      });
    }

    function hora(tempo){
      //Live update das informacoes do sensor
      function loadData() {
        $.getJSON('loadchart.php', function(response) {
          myLineChart.data.datasets[0].data = response[0];
          myLineChart.data.datasets[1].data = response[1];
          myLineChart.data.datasets[2].data = response[2];
          myLineChart.data.datasets[3].data = response[3];
          myLineChart.data.labels = response[4];
          myLineChart.update();
        });
      }

      loadData();
      loadHour();
      setInterval(loadData, 5000);
      setInterval(loadHour, 5000);

You need to do something like this.你需要做这样的事情。

function loadData()
{
   $.ajax({
      type: 'POST',
      url: 'loadchart.php',
      dataType: 'json',
      success: updateChart
   });
}

function updateChart(res)
{
   // manipulate the responded json here
}

setInterval(loadData,5000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM