2012-01-09 83 views
0

我不能修復這個爲什麼只更新了最後一個標記(ID),而不是一個接一個 這我我的第一個樂趣(),它與file.svg的onload事件開始:自動更新腳本JS/SVG

function refresh() 
{ 
    totalUpdate(); 
    var t=setTimeout("refresh()",2000); 
} 

這裏的功能是:

function totalUpdate() 
{ 
    //user array with elements on user ID 
    var tags = document.getElementById("user").getElementsByTagName("circle"); 

    for(var i=0;i<=document.getElementById('user').childElementCount;i++) 
    { 

     var tag=null,id=null,url=null; 
     id=tags[i].getAttribute("id"); 
     tag=tags[i]; 
     url="get.php?id="+id; 
     $.get(url,function(data){ 
      updateDATA(tag, data); 
     }); 
    } 
} 

function updateDATA(tag, data){ 
    data=data.split(" "); 
    //data[0]=>x data[1]=>y data[2]=>data 
    var x,y; 

    //$x=(($x*1000000-$bx)*$svgwidth/$mx*-1)*1000000; 
    x=((data[0]*1000000-20959350)*352/3615); 
    y=((data[1]*1000000-41989603)*662/5391)*-1; 

    tag.setAttribute("cx",x+4); 
    tag.setAttribute("cy",y+5); 
} 

SVG體:

<g id='user'> 


    <circle class="user" id="1" cy="329" cx="179" r="5" style="fill:red"> 
     <animate attributeType="CSS" attributeName="opacity" from="1" to="0" dur="1s" repeatCount="indefinite" /> 
    </circle> 


    <circle class="user" id="2" cy="366" cx="189" r="5" style="fill:green"> 
     <animate attributeType="CSS" attributeName="opacity" from="1" to="0" dur="1s" repeatCount="indefinite" /> 
    </circle> 
    </g> 

功能僅更新的第二個元素不是兩個!?

回答

0

Javascript有詞法作用域和$.get是異步的,因此在調用updateDATA時引用tag引用列表tags中的最後一個元素。你可以通過用函數包裝異步調用來解決這個問題。

看看這個演示,其中$.getsetTimeout取代:http://jsfiddle.net/GuGb4/