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.
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21
  1. #16
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Code:
    function switchImage() 
    {
      var currentIndex = 1;
      var totalIndex = 3;
      
      function f()
      {
        $(".content").css('background-image', 'url("images/'+currentIndex+'.png")');
    
        if (++currentIndex > totalIndex) 
        {
           currentIndex = 1;
        }
      }
    
      setInterval( f, 3000 );
    }
    
    function windowOnload()  /* presumably this is called by an onload handler */
    {
       switchImage();
    }

  2. #17
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Quote Originally Posted by krillezzz View Post
    I just thought i solved it...i had these two global variables local instead.

    Code:
    function switchImage() 
    {
    	var currentIndex = 1;
    	var totalIndex = 3;
    	currentIndex++;
    	$(".content").css('background-image', 'url("images/'+currentIndex+'.png")');
    	if (currentIndex == totalIndex) 
    	{
    		currentIndex = 1;
    	}
    }
    
    function windowOnload() 
    {
    	setInterval("switchImage()", 3000);
    }
    When i load my webpage it switches to from the first to the second image...but there it stops
    It is stopping at the second image because the variables are now local, so everytime that function is called you are setting currentIndex back to 1 so it only ever increments to 2.

    Regards,

    LC.

  3. #18
    New Coder
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by blaze4218 View Post
    is it because you forgot the kontor-?
    again:
    Code:
    setInterval("switchImage()", 3000);
    should read
    Code:
    setInterval( switchImage , 3000 );
    I'm sorry, I forgot to say that I changed the name of images to 1, 2 and 3.png

    Quote Originally Posted by Logic Ali View Post
    Code:
    function switchImage() 
    {
      var currentIndex = 1;
      var totalIndex = 3;
      
      function f()
      {
        $(".content").css('background-image', 'url("images/'+currentIndex+'.png")');
    
        if (++currentIndex > totalIndex) 
        {
           currentIndex = 1;
        }
      }
    
      setInterval( f, 3000 );
    }
    
    function windowOnload()  /* presumably this is called by an onload handler */
    {
       switchImage();
    }
    But when I had those variables as global the function didn't work at all, when they are local it switches to picture number 2. So i thought it was one step closer :P

  4. #19
    New Coder
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I solved to problem (thanks to you guys), but there was a problem that i didn't expect.

    our teacher said that in the top of the javascript-file we should have the line
    Code:
    jQuery.support.cors = true;
    to make it support jQuery, i don't know why but this line made all my global variables f*cked up :P so they didn't work. I just comment that line away and wops it started to work, anyone know why?

  5. #20
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Where exactly have you put that line of code?

    Regards,

    LC.

  6. #21
    New Coder
    Join Date
    Sep 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by LearningCoder View Post
    Where exactly have you put that line of code?

    Regards,

    LC.
    In top of the javascript-file


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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