0

I'm trying to change src attribute of an image then restore the default value using setInterval()method but it seems that if I use the variable inside the function it doesn't work properly and It must be outside the function, why the variable value wont change inside the function?

var Img1 = document.getElementById('img1');

setInterval(function(){
    var boolean= true;
    if(boolean){
        Img1.src = "pic1.jpg";
        }else{
        Img1.src = "default.jpg";
    }
    boolean= !boolean;
} , 3000);



// This one work properly!

var Img1 = document.getElementById('img1');
var boolean= true;

setInterval(function(){

    if(boolean){
        Img1.src = "pic1.jpg";
        }else{
        Img1.src = "default.jpg";
    }
    boolean= !boolean;
} , 3000);
4
  • 9
    Because you're redeclaring it every time you run the function. The very first line in your function is boolian = true so it's going to set it to true. Commented Aug 3, 2016 at 20:44
  • Btw, a shorter way to do that is boolean ^= true; Commented Aug 3, 2016 at 20:51
  • @4castle that seems to just always set boolean to 0, am I missing something? Commented Aug 3, 2016 at 20:57
  • @RobM. Whoops, my Java is bleeding into JavaScript. It would just flip back and forth between 1 and 0. (for this purpose though, that would still work as a boolean) So the shortest would be boolean ^= 1; Commented Aug 3, 2016 at 20:59

2 Answers 2

1

That is because boolean is always true whenever the function get called. That is the first thing the function does, set boolean to true:

var boolean= true;

boolean does not get modified inside the function until after you check to see which src to choose. Thus, your condition always arrive to the same outcome, which is to set Img.src to pic1.jpg. Hope that helps!

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

Comments

0

Within the function sent to setInterval(), you are setting "boolean" to be true at the start of every iteration of the function. Thus the following code will always set Img1.src = "pic1.jpg"; and the line Img1.src = "default.jpg"; will never get run because "boolean" is true in every case of the iteration.

setInterval(function(){
    var boolean= true;
    if(boolean){
        Img1.src = "pic1.jpg";
    }else{
        Img1.src = "default.jpg";
    }
    boolean= !boolean;
} , 3000);

You want to wrap your setInterval in a function so that you can modify the boolean value per iteration and that the value setDefault is scoped to the caller. For instance:

const IMAGE_DEFAULT = "default.jpg";
const IMAGE_PIC1 = "pic1.jpg";

function StartImageInterval(img) {
    var setDefault = true;

    setInterval(function() {
        img.src = ((setDefault) ? IMAGE_DEFAULT : IMAGE_PIC1);
        setDefault = !setDefault;
    }, 3000);
}

You should then be able to call this function with

var Img1 = document.getElementById('img1');
StartImageInterval(Img1);

Comments

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.