<head>
</head>
<body>
<?xml version="1.0" encoding="gb2312"?>
<xml id="Login">
   <Students name="98班级">
 <Student name="王小平">
  <Chinese id="語文">
     <Item id="单选题">
       <Score id="33"></Score>
       <Score id="33"></Score>
       <Score id="33"></Score>
     </Item>
     <Item id="多选题">
       <Score id="33"></Score>
   </Item>
     <Item id="计算题">
       <Score id="33"></Score>
     </Item>
     <Item id="应用题">
       <Score id="33"></Score>
     </Item>
     <Item id="附加题">
       <Score id="33"></Score>
     </Item>
  </Chinese>
  <Physics id="物理"></Physics>
  <English id="英語"></English>
  <Geography id="地理"></Geography>
  <Historical id="歷史"></Historical>
 </Student>
 <Student name="孟雲江">
  <Chinese id="語文"></Chinese>
  <Physics id="物理"></Physics>
  <English id="英語"></English>
  <Geography id="地理"></Geography>
  <Historical id="歷史"></Historical>
 </Student>
 <Student name="胡光">
  <Chinese id="語文"></Chinese>
  <Physics id="物理"></Physics>
  <English id="英語"></English>
  <Geography id="地理"></Geography>
  <Historical id="歷史"></Historical>
 </Student>
</Students>
</xml>
  </xml>
  
<head></head>
<script>
var Box = function (){}
var Tree = function ()
{
    this.create = function (level,xml)
    {
        var space = "";
  
        for(var i=0;i<level;i++)
        {
            space += "  ";
        }
  
  if(level != 0)
         document.body.innerHTML += space + xml.attributes[0].value +"<BR>";
         for(var i=0;i< xml.childNodes.length;i++)
         {
            if(xml.childNodes[i].childNodes.length>0)
            {
                this._tree = new Box();
                Tree.apply(this._tree);
                this._tree.create(level+1,xml.childNodes[i]);
            }
            else
            {
                document.body.innerHTML += space + xml.childNodes[i].attributes[0].value +"<BR>";
            }
        }
    }
}var tree = new Box();
Tree.apply(tree);
tree.create(0,Login.documentElement);
</script>
</body>
上面的代码,我想实现每隔 1 秒读出一个 xml节点并输出到屏幕上(innerHTML),不知道应怎样设置 settimeout 请大家帮忙,谢谢

解决方案 »

  1.   

    用setinterval(),当解析完最后一个就可以清除 句柄
      

  2.   

    setinterval()或setTimeout()都可以.关键是得改变程序结构,在延时执行函数中每次取并输出一个节点.可以节点的兄弟节点和父节点子节点这些关联对象来操作,也可以记录上一次子节点的索引值.都可以.
      

  3.   


    <head>
    </head>
    <body>
    <?xml version="1.0" encoding="gb2312"?>
    <xml id="Login">
       <Students name="98班级">
     <Student name="王小平">
      <Chinese id="語文">
         <Item id="单选题">
           <Score id="33"></Score>
           <Score id="33"></Score>
           <Score id="33"></Score>
         </Item>
         <Item id="多选题">
           <Score id="33"></Score>
       </Item>
         <Item id="计算题">
           <Score id="33"></Score>
         </Item>
         <Item id="应用题">
           <Score id="33"></Score>
         </Item>
         <Item id="附加题">
           <Score id="33"></Score>
         </Item>
      </Chinese>
      <Physics id="物理"></Physics>
      <English id="英語"></English>
      <Geography id="地理"></Geography>
      <Historical id="歷史"></Historical>
     </Student>
     <Student name="孟雲江">
      <Chinese id="語文"></Chinese>
      <Physics id="物理"></Physics>
      <English id="英語"></English>
      <Geography id="地理"></Geography>
      <Historical id="歷史"></Historical>
     </Student>
     <Student name="胡光">
      <Chinese id="語文"></Chinese>
      <Physics id="物理"></Physics>
      <English id="英語"></English>
      <Geography id="地理"></Geography>
      <Historical id="歷史"></Historical>
     </Student>
    </Students>
    </xml>
      </xml>
      
    <head></head>
    <script>
    var info=[];//先将递归遍历得到的结果存入该数组,等遍历结束在每隔1s显示
    var index=0;
    var showInfo=function () {
        if (info[index]) {
            document.body.innerHTML +=info[index];
            index++;
            setTimeout(showInfo,1000);
        }    
    }
    var Box = function (){}
    var Tree = function ()
    {
        this.create = function (level,xml)
        {
            var space = "";
      
            for(var i=0;i<level;i++)
            {
                space += "  ";
            }
      
      if(level != 0)
             //document.body.innerHTML += space + xml.attributes[0].value +"<BR>";
             info.push(space + xml.attributes[0].value +"<BR>");
             for(var i=0;i< xml.childNodes.length;i++)
             {
                if(xml.childNodes[i].childNodes.length>0)
                {
                    this._tree = new Box();
                    Tree.apply(this._tree);
                    this._tree.create(level+1,xml.childNodes[i]);
                }
                else
                {
                    //document.body.innerHTML += space + xml.childNodes[i].attributes[0].value +"<BR>";
                    info.push(space + xml.childNodes[i].attributes[0].value +"<BR>");
                }
            }
        }
    }
    var tree = new Box();
    Tree.apply(tree);
    tree.create(0,Login.documentElement);
    setTimeout(showInfo,1000);
    </script>
    </body>