duanchuang6978 2015-10-28 04:32
浏览 22
已采纳

用于Morris图数据的jQuery post的setInterval

I'm using jQuery to post and retrieve data from my controller (codeigniter). I've no problem with retrieving data. I want to use setInterval to update the morris data and make the morris data (JSON data) dynamic. The problem is when I use the setinterval to resend the update request to controller the graph disappear! I'm using the codeigniter output:

$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($data1['query'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;

And this is the JavaScript code that I'm trying to use:

var json = (function () {
        var json = null;
        setInterval(function() {
        $.post( "/login/DailyAverageWaitingtime", function( data ) {
            json = data;
        });
        }, 5000);
        return json;
    })
    ();

    Morris.Area({
    element: 'DailyAverageWaitingtime',
    padding: 10,
    behaveLikeLine: true,
    gridEnabled: false,
    gridLineColor: '#dddddd',
    axes: true,
    fillOpacity:.7,
    data:json.result_object,
    lineColors:['#ED5D5D','#D6D23A','#32D2C9'],
    xkey: 'date',
    ykeys:['records','waiting'],
    labels: ['Records','Waiting'],

    pointSize: 0,
    lineWidth: 0,
    hideHover: 'auto'
});
  • 写回答

2条回答 默认 最新

  • dqqg0811 2015-10-28 09:32
    关注

    Finally I fixed the issue!

    This is a temporary function to create the graph at first place:

    function DailyAverageWaitingtime(){
        var DailyAverageWaitingtime;
            $.post( "/login/DailyAverageWaitingtime", function( data ) {
                DailyAverageWaitingtime = Morris.Area({
                    element: 'DailyAverageWaitingtime',
                    data: data.result_object,
                    xkey: 'date',
                    ykeys: ['records', 'waiting'],
                    labels: ['Records', 'Waiting'],
                    pointSize: 2,
                    hideHover: 'auto',
                    resize: true
                });
    

    Interval to call the update function:

                setInterval(function() { UpdateDailyAverageWaitingtime(DailyAverageWaitingtime); }, 20000);
            }, "json"); 
    }
    

    And this is to update the data every X second:

    function UpdateDailyAverageWaitingtime(DailyAverageWaitingtime){
        $.post( "/login/DailyAverageWaitingtime", function( data ) {
            DailyAverageWaitingtime.setData(data.result_object);
            myRecords.setData(data.result_object);
    
                });
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符