Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
JavaScript
var scrollSpeed = 50; 		// Speed in milliseconds
var step = 1; 			// How many pixels to move per step		    
var current = 0;		// The current pixel row
var imageHeight = 393;		// Background image height
var headerHeight = 300;		// How tall the header is.
			
//The pixel row where to start a new loop
var restartPosition = -(imageHeight - headerHeight);
			
function scrollBg(){
				
	//Go to next pixel row.
	current -= step;
				
	//If at the end of the image, then go to the top.
	if (current == restartPosition){
			current = 0;
		}
			
		//Set the CSS of the header.
		$('#header').css("background-position","0 "+current+"px");
				
		}
                      //Calls the scrolling function repeatedly
			var init = setInterval("scrollBg()", scrollSpeed);

In this code the interval is keep repeating but what i want it to stop at the position where 1st interval end.
If any of you can modify this loop then please do.

thanks
Posted

See,

the below line of code only calling the function repeatedly, execute this line of code inside of a condition or if you want to execute only one time just comment this line of code.

C#
//Calls the scrolling function repeatedly
var init = setInterval("scrollBg()", scrollSpeed);
 
Share this answer
 
Comments
[no name] 8-May-13 1:22am    
hi,
I did comment but its not working for even once.
Thanks
I think you must call clearInterval function.

Example usage here:

http://www.w3schools.com/jsref/met_win_clearinterval.asp[^]
 
Share this answer
 
You can use setTimeout() here, like this:

JavaScript
var init = setTimeout("scrollBg()", scrollSpeed);
clearTimeout(init);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900