当前位置: 代码迷 >> Java Web开发 >> js求循环解决办法
  详细解决方案

js求循环解决办法

热度:32   发布时间:2016-04-13 22:12:40.0
js求循环

怎么在读完数据后循环再度一遍
就是这个var x1=[155,165,496,454,123];

var chart;                                                                  
$('#container').highcharts({                                                
chart: {                                                                
type: 'spline',                                                     
animation: Highcharts.svg, // don't animate in old IE               
marginRight: 10,                                                    
events: {                                                           
load: function() {                                              
// set up the updating of the chart each second             
var series = this.series[0]; 
window.onload = test;
  var sh;
var x1=[155,165,496,454,123];
var t = 0;
  var num = 0;
function test() {
         sh = window.setInterval(show,2000);
 }   
function show() {
         if (t == x1.length) {
                 clearInterval(sh);
                 return;
         }
         num = x1[t];
         t++;
}                         
setInterval(function() {                                    
var x = (new Date()).getTime(), // current time         
y = num;                            
series.addPoint([x, y], true, true);          
}, 2000);                                                   
}                                                               
}                                                                   
},

------解决思路----------------------
把获取数据和setInterval放到外面,然后通过highchart的api去刷新图像
------解决思路----------------------
引用:
完了直接结贴
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <title></title> 
  <script type="text/javascript" src="http://static.hcharts.cn/jquery/jquery-2.0.2.min.js"></script> 
  <script type="text/javascript" src="http://www.hcharts.cn/demo/js/highcharts.js"></script> 
  <script type="text/javascript">
$(document).ready(function() {                                                  
    Highcharts.setOptions({                                                     
        global: { 
            useUTC: false                                                       
        }                                                                       
    });                                                                         
             
                                                                                         
    var chart;                                                                  
    $('#container').highcharts({                                                
        chart: {                                                                
            type: 'spline',                                                     
            animation: Highcharts.svg, // don't animate in old IE               
            marginRight: 10,                                                    
            events: {                                                           
                load: function() {                                                                                                         
                    // set up the updating of the chart each second             
                    var series = this.series[0]; 
                    var x1=[155,165,496,454,123];
                    var i=0;
                   
                   window.setInterval(show,1000);
                       
                    function show() {
             if (i == x1.length) {
                     i=0;
             }                                   
                                                       
                        var x = (new Date()).getTime(), // current time         
                            y = x1[i++];                            
                        series.addPoint([x, y], true, true);   }       
                                                                     
               }                                                               
            }                                                                   
        },                                                                      
        title: {                                                                
            text: '1秒'                                            
        },                                                                      
        xAxis: {           
            type: 'datetime',                                                   
            tickPixelInterval: 100                                              
        },                                                                      
        yAxis: {    
        //背景线
        gridLineWidth:'0px',                                                            
            title: {                                                            
                text: '单位:M'                                                  
            },                                                                  
            plotLines: [{                                                       
                value: 0,                                                       
                width: 1,                                                       
                color: '#808080' 
                                                                
            }]                                                                  
        },                                                                      
        tooltip: {                                                              
            formatter: function() {                                             
                    return '<b>'+ this.series.name +'</b><br/>'+                
                    Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
                    Highcharts.numberFormat(this.y, 2);                         
            }                                                                   
        },                                                                      
        legend: {                                                               
            enabled: false                                                      
        },                                                                      
        exporting: {                                                            
            enabled: false                                                      
        },                                                                      
        series: [{                                                              
            name: 'Random data',                                                
            data: (function() {                                                 
                // generate an array of random data                             
                var data = [],                                                  
                    time = (new Date()).getTime(),                              
                    i;                                                          
                                                                                 
                for (i = -19; i <= 0; i++) {                                    
                    data.push({                                                 
                        x: time + i * 1000,                                     
                        y: Math.random()*1000                                    
                    });                                                         
                }                                                               
                return data;                                                    
            })()                                                                
        }]                                                                      
    });                                                                         
});                                                                             
                                                                                 
</script> 
 </head> 
 <body> 
  <div id="container" style="width:80%;height:100%;margin:0 auto;"></div> 
  <div style="text-align:center;clear:both;"> 
  </div>  
 </body>
</html>
   你的引用不然别人不知道
  相关解决方案