2009-12-24 4 views
0

이 함수가 id에 변수를 사용하도록하려면 어떻게해야합니까? 내가 할 때 setTimeout을 사용할 수 없습니다.'setTimeout'함수에 인수를 전달하는 방법은 무엇입니까?

<script type="text/javascript"> 
     function changeBarAWidth() { 
      var bar = document.getElementById('firstbar'); 
      bar.style.width = lengthA + "px"; 
     } 

     function increaseBarA() { 
      if (lengthA < fullBarA) { 
       changeBarAWidth(); 
       lengthA = (lengthA + 2); 
       setTimeout("increaseBarA()", 10); // <-- How do I pass "firstbar"? 
      } 
     } 

     function resetLengthA() { 
      lengthA = 0; 
      changeBarAWidth(); 
     } 

     var barA = 'firstbar'; 
     var percentA = 80; 
     var fullBarA = (percentA * 2); 
     var lengthA = 0; 
    </script> 
    <style type="text/css"> 
     ul { 
      list-style-type: none; 
     } 
     .bar { 
      width: 50px; 
      height: 5px; 
      background-color: red; 
     } 
     .back { 
      background-color: black; 
      height: 5px; 
      width: 200px; 
     } 
    </style> 
</head> 
<body onload="increaseBarA()"> 
    <ul> 
     <li class="back"> 
      <p class="bar" id="firstbar"></p> 
     </li> 
    </ul> 
    <button onClick="increaseBarA()">Test Change</button> 
    <button onClick="resetLengthA()">Reset</button> 
+1

코드 편집기에서 적절한 들여 쓰기 및 서식을 구성하십시오. – BalusC

답변

0

나는 setTimeout(function() { YourFuncwith(params); }, 10);을 사용해야했습니다.

다음은 스크립트입니다. 누군가가 사본을 원하는 경우, 제발 :

// functions to show poll reults, div width increase every tenth of a second, 
 
// looks like poll is sliding. 
 

 
// length is name of global length variable you declare, mine are at bottom of script 
 
// I will use php to dynaimcally give variable values based on poll results. 
 
function changeBarWidth(id, length) { 
 
    var bar = document.getElementById(id); 
 
    bar.style.width = length + "px"; 
 
} 
 

 

 
/* max is how long you want the poll to slide to, my background is 200px 
 
* so I set to (percent*2); 
 
*/ 
 
function increaseBar(id, max, length) { 
 
    var full = max; 
 

 
    if (length < full) { 
 
     changeBarWidth(id, length); 
 
     length = (length + 2); 
 

 
     setTimeout(function() { 
 
      increaseBar(id, max, length); 
 
     }, 10); 
 
    } 
 
} 
 

 
var barA = 'firstbar'; 
 
var percentA = 80; 
 
var fullBarA = (percentA * 2); 
 
var lengthA = 0; 
 
var lengthB = 0;
ul { 
 
    list-style-type: none; 
 
} 
 
.bar { 
 
    width: 50px; 
 
    height: 5px; 
 
    background-color: red; 
 
} 
 
.back { 
 
    background-color: black; 
 
    height: 5px; 
 
    width: 200px; 
 
}
<body onload="increaseBar(barA, percentA, lengthA)"> 
 
    <div id="pollresults"> 
 
     <ul> 
 
      <li class="back"> 
 
       <p class="bar" id="firstbar"></p> 
 
      </li> 
 
      <li class="back"> 
 
       <p class="bar" id="secondbar"></p> 
 
      </li> 
 
     </ul> 
 
    </div> 
 
    <button onClick="increaseBar('secondbar',fullBarA,lengthB)">Test Change</button> 
 
</body>

관련 문제