Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    well, first off, you are defining img as a function, then defining img within that function, as a variable .

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i spotted that earlier and fixed it but it had no effect, i updated the code

  • #4
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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();
    }

  • Users who have thanked DanInMa for this post:

    barrerr122 (12-07-2012)

  • #5
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    224
    Thanks
    0
    Thanked 32 Times in 30 Posts
    Is it really necessary to define a function get ?

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

  • #6
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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?

  • #8
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •