简体   繁体   English

自动更新脚本js / svg

[英]Auto update script js/svg

I can not fix this why it update only the last tag(id), not one-by-one This i my first fun() which start with onload of file.svg: 我不能解决这个问题,为什么它只更新最后一个tag(id),而不是一个一个地更新。这是我的第一个fun(),它从file.svg的加载开始:

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

And here are the functions: 这里是函数:

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 body: 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>

Functions updating only the second element not both of them!? 函数仅更新第二个元素,而不是全部更新!

Javascript has lexical scoping and $.get is asynchronous, so by the time updateDATA is invoked tag references the last element in the list tags . Javascript具有词法作用域,并且$.get是异步的,因此在调用updateDATAtag引用list tags的最后一个元素。 You can fix this by wrapping the async call with a function. 您可以通过使用函数包装异步调用来解决此问题。

Check out this demo where $.get is replaced by setTimeout : http://jsfiddle.net/GuGb4/ 查看此演示,其中$.getsetTimeout替换: http : //jsfiddle.net/GuGb4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM