本文介绍了如何在下面提到的代码中实现轮询。我想每60秒从指定的网址轮询数据。我该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <!DOCTYPE html> 
< html>
< head>
< script src =https://code.jquery.com/jquery-3.1.1.min.js>< / script>
< script src =https://code.highcharts.com/highcharts.js>< / script>
< script src =https://code.highcharts.com/highcharts-more.js>< / script>
< script src =https://code.highcharts.com/modules/exporting.js>< / script>


< / head>
< body>

< div id =containerstyle =min-width:310px; max-width:600px; height:400px; margin:0 auto>时间系列Highchart< / div>

< script>
$(document).ready(function(){

var options = {
chart:{
renderTo:'container',
type: 'spline'
},
series:[{}]
};


setInterval(function(){
$ .getJSON ('https://www.highcharts.com/samples/data/jsonp.php?filename=usdeur.json&callback=?',函数(数据){
options.series [0] .data = data ;
var chart = new Highcharts.Chart(options);
});
},60000);

});
< / script>
< / body>
< / html>

这是打印剩下的api并给出highchart数据的代码。我想要实现这些代码,以便它能够在轮询后至少60秒后在highchart上显示数据。

使用setInterval函数:
$(document).ready(function(){

  var options = {
图表:{
renderTo:'container',
type:'spline'
},
series:[{}]
};
var chart = new Highcharts.Chart(options);

setInterval(function(){
$ .getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename = usdeur.json& callback =?',function(data){
chart.series [0] .setData(data);
});
},60000);

编辑:添加图表更新。


kindly help me to implement polling in below mentioned code.

    <!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>


</head>
<body>

<div id="container" style="min-width: 310px; max-width: 600px; height: 400px; margin: 0 auto">Time series Highchart</div>

<script>
$(document).ready(function() {

    var options = {
        chart: {
            renderTo: 'container',
            type: 'spline'   
        },
        series: [{}]
    };


    setInterval(function(){
        $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=usdeur.json&callback=?', function(data) {
               options.series[0].data = data;
               var chart = new Highcharts.Chart(options);
        });
    }, 60000);

});
</script>
</body>
</html>

This is the code which is hitting the rest api and giving the data on highchart. I want to implement the code so that it will show the data on highchart after polling for atleast 60 sec.

解决方案

Use the setInterval function: $(document).ready(function() {

var options = {
    chart: {
        renderTo: 'container',
        type: 'spline'   
    },
    series: [{}]
};
var chart = new Highcharts.Chart(options);

setInterval(function(){
    $.getJSON('https://www.highcharts.com/samples/data/jsonp.php?filename=usdeur.json&callback=?', function(data) {
        chart.series[0].setData(data);
     });
 }, 60000);

Edit: added chart update.

这篇关于如何在下面提到的代码中实现轮询。我想每60秒从指定的网址轮询数据。我该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 14:03