0

I'm tasked with creating a progress bar (actually, a timer that looks like a progress bar) that will be triggered on a click event. I need multiple bars as a person may click on multiple elements that will trigger the progress bar. This is the script I have now:

function createPbar(IDofpBar, timeOut, timeIncrement ) {

            ...grab dom elements and do the math...

    i=0;
    var newWidth = 0;   

    var x = setInterval(function(){
        theBar.style.width = newWidth+"px"
        newWidth = newWidth + bIncrement;
        if (i == counter) {
            clearInterval(x);
        }
        i++
    }, timeIncrement*1000);

}

This works fine until I have more than one on the page at a time. When I trigger the second one, it affects the first one. I'm guessing it's due to reassigning the variables each time I trigger a new progress bar.

Is there a way to isolate the variables per call?

2 Answers 2

4

Use var to declare variables within the scope of the current function, rather than in the global scope. You're missing a var before the i=0; statement. I don't see the code that initializes theBar, but you might be missing a var there, too.

function createPbar(IDofpBar, timeOut, timeIncrement ) {

    /* ...grab dom elements and do the math... */

    var theBar = ...,
        i=0,
        newWidth = 0;

    var x = setInterval(function(){
        theBar.style.width = newWidth+"px"
        newWidth = newWidth + bIncrement;
        if (i == counter) {
            clearInterval(x);
        }
        i++
    }, timeIncrement*1000);
}

Just in case: var keyword @ MDC.

Sign up to request clarification or add additional context in comments.

1 Comment

argh! Yep. That was the culprit.
1

Matt's answer is correct, but I'll expand on it a bit further.

In javascript assigning a variable without using the var keyword creates the variable in the global scope. In your current code this means that all instances of your progress bar are going to be referencing and accessing the same instance of i.

By using the var keyword to declare variables inside of a function, it declares the variable to have local scope and makes it only accessible from within the function. This allows you to have independent versions of the variables used in your function.

This is a gotcha which I often find myself having to go back and fix when I forget.

2 Comments

I appreciate the explanation!
It's not correct though. Undeclared variables are created as properties of the global object when they are assigned values, attempting to use them before then will create a reference error like "i is not defined". Declaring variables means they are available before code execution begins, preventing such errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.