English 中文(简体)
How do I pass arguments to a setTimeout function?
原标题:

How can I make these functions use variables for the id? I can t get the setTimeout to work when I do.

    <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>
问题回答

I had to use a setTimeout(function() { YourFuncwith(params); }, 10);.

Here is the script. If anybody would like copy, please do:

// 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>




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签