...

View Full Version : Weird javascript error, if() not working



barrerr122
12-07-2012, 01:19 PM
im getting a weird runtime error where this:


if(img==5){
img=1;
}else{
img=img+1;
}
is not doing anything as it runs neither option, i know it's here because i tracked it with alerts. heres the rest of the code if you need it:
var img=1;
var source1;
var source2;
var called=0;
var x=1;

function get(){
if(img==1){
source1="Images/test1.jpg";
}else if(img==2){
source1="Images/test2.jpg";
}else if(img==3){
source1="Images/test3.jpg";
}else if(img==4){
source1="Images/test4.jpg";
}else if(img==5){
source1="Images/test5.jpg";
}
}

$(document).ready(function(){
setup();

});

function setup() {
get();
document.getElementById("imgm").src=source1;
if(img==5){
img=1;
}else{
img=img+1;
}
get();
if(called==0){
source2=source1;
}else(
setTimeout(anim(), 5000);
}
called=1;
}


function anim(){
$("imgm").fadeOut("slow");
document.getElementById("imgm").src=source2;
$("imgm").fadeIn("slow");
alert("test");
setup();
}


I would llove if someone could explain to me what i did wrong so i can fix it, thanks

DanInMa
12-07-2012, 02:08 PM
well, first off, you are defining img as a function, then defining img within that function, as a variable .

barrerr122
12-07-2012, 02:11 PM
i spotted that earlier and fixed it but it had no effect, i updated the code

DanInMa
12-07-2012, 02:40 PM
give this a try:'





var img=1;
var imgUrl;
var imgCount =5;


$(document).ready(function(){
document.getElementById("imgm").src="Images/test1.jpg"; // setup first image on pageload
setup();

});

function setup() {
if(img>imgCount){ // if img is greater than the value of imgCount set to 1
img=1;
}else{
img++;//add 1 to the current numeric value
}
imgUrl="Images/test"+img+".jpg";//set imgUrl based on current img numeric value
setTimeout(anim(), 5000);
}


function anim(){
$("#imgm").fadeOut("slow",function(){//fade out element with an id of "imgm"
$(this).attr('src',imgUrl).fadeIn("slow");//once fadeout has completed, change src attribute of "imgm" to value of imgUrl, then fade in
});
alert("test");
setup();
}

007julien
12-07-2012, 02:44 PM
Is it really necessary to define a function get ?

source1="Images/test"+(img)+".jpg";

barrerr122
12-07-2012, 03:11 PM
ok it seems to just not display the image now, however the alert() is showing so assumably it should be working. also the get() function was from an earlier program where the sources all had different names instead of test. anyway thanks for the help, ill see if i can get this to work,might just be the crappy computer im working on though im not sure if that would have an effect.

DanInMa
12-07-2012, 03:16 PM
dont give up so easily. if its not working there msut be a reason. can you show use more of the html for the page? how about the html for the img element?

barrerr122
12-07-2012, 03:17 PM
oh god no im not giving up, im just trying to do as much for myself as i can, the less other people do for me the more i'll learn. but thank you



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum