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 09-17-2012, 06:54 PM   PM User | #16
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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();
}
Logic Ali is offline   Reply With Quote
Old 09-17-2012, 07:09 PM   PM User | #17
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
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.
LearningCoder is offline   Reply With Quote
Old 09-17-2012, 07:26 PM   PM User | #18
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
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
krillezzz is offline   Reply With Quote
Old 09-18-2012, 03:42 PM   PM User | #19
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
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?
krillezzz is offline   Reply With Quote
Old 09-18-2012, 06:37 PM   PM User | #20
LearningCoder
Regular Coder

 
LearningCoder's Avatar
 
Join Date: Jan 2011
Location: The Pleiades
Posts: 849
Thanks: 67
Thanked 28 Times in 28 Posts
LearningCoder is an unknown quantity at this point
Where exactly have you put that line of code?

Regards,

LC.
LearningCoder is offline   Reply With Quote
Old 09-18-2012, 09:52 PM   PM User | #21
krillezzz
New Coder

 
Join Date: Sep 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
krillezzz is an unknown quantity at this point
Quote:
Originally Posted by LearningCoder View Post
Where exactly have you put that line of code?

Regards,

LC.
In top of the javascript-file
krillezzz 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 12:06 AM.


Advertisement
Log in to turn off these ads.