Jump to content

condtional if-else


Eep²

Recommended Posts

I'm trying to make a script that will change an image several times after the image is clicked. So, if the image is clicked once, the image changes, if clicked again, the image changes again, etc. I'm trying to use a variable that increases in amount but it doesn't seem to be working for some reason. Any ideas? Thanks.

function changepic(img_name,img_src) {document[img_name].src=img_src;}

function changepic1()
{
	var o = 0;
	if (!o)
	{
		changepic('circles','circular_infinity1.png');
		o++;
	}
	else if (o==1)
	{
		changepic('circles','circular_infinity_intersected1a.png');
		o++;
	}
	else {changepic('circles','circular_infinity1a.png')}
	o = !o
}
Link to comment
Share on other sites

This is how I script in Second Life and it works fine, except for this change:

function changepic(img_name,img_src) {document[img_name].src=img_src;}

function changepic1()
{
	var o = 0;
	if (!o)
	{
		changepic('circles','circular_infinity1.png');
		o++;
	}
	else if (o==1)
	{
		changepic('circles','circular_infinity_intersected1a.png');
		o++;
	}
	else
        {
                changepic('circles','circular_infinity1a.png');
	        o = 0;
        }
}

But it still doesn't work past the first if. The boolean operator (==) is used to make sure o does indeed equal a specific #. Regardless, just using "=" doesn't work either. A dynamic local variable can or can't be zero; hence why I test for it in the beginning to begin the if-elses.

 

Got any actual answers instead of questions?

Link to comment
Share on other sites

o will always be zero, because you just set it to zero, and zero is a falsy value, so !o will always yield a truthy value.

 

Declare and initialize o outside the function, and don't use ! on a number.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...