Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2012, 01:19 PM   PM User | #1
barrerr122
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
barrerr122 is an unknown quantity at this point
Weird javascript error, if() not working

im getting a weird runtime error where this:
Code:
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:
Code:
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

Last edited by barrerr122; 12-07-2012 at 02:10 PM..
barrerr122 is offline   Reply With Quote
Old 12-07-2012, 02:08 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
well, first off, you are defining img as a function, then defining img within that function, as a variable .
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is online now   Reply With Quote
Old 12-07-2012, 02:11 PM   PM User | #3
barrerr122
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
barrerr122 is an unknown quantity at this point
i spotted that earlier and fixed it but it had no effect, i updated the code
barrerr122 is offline   Reply With Quote
Old 12-07-2012, 02:40 PM   PM User | #4
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
give this a try:'



Code:
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();
}
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is online now   Reply With Quote
Users who have thanked DanInMa for this post:
barrerr122 (12-07-2012)
Old 12-07-2012, 02:44 PM   PM User | #5
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
Is it really necessary to define a function get ?

source1="Images/test"+(img)+".jpg";
007julien is offline   Reply With Quote
Old 12-07-2012, 03:11 PM   PM User | #6
barrerr122
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
barrerr122 is an unknown quantity at this point
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.
barrerr122 is offline   Reply With Quote
Old 12-07-2012, 03:16 PM   PM User | #7
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,307
Thanks: 12
Thanked 204 Times in 204 Posts
DanInMa is on a distinguished road
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?
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is online now   Reply With Quote
Old 12-07-2012, 03:17 PM   PM User | #8
barrerr122
New to the CF scene

 
Join Date: Dec 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
barrerr122 is an unknown quantity at this point
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
barrerr122 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 AM.


Advertisement
Log in to turn off these ads.