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);
boolian = trueso it's going to set it totrue.boolean ^= true;booleanto0, am I missing something?boolean ^= 1;