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条)

报告相同问题?

悬赏问题

  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Linux权限管理相关操作(求解答)
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表
  • ¥15 DbVisualizer Pro 12.0.7 sql commander光标错位 显示位置与实际不符
  • ¥15 android 打包报错