Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple Javascript Loop

Posted on 2010-09-14
4
Medium Priority
?
226 Views
Last Modified: 2012-05-10
I have a list of five anchors.
On page load, I want to cycle through this list and add/remove an underline to each anchor, one at a time.
I want to delay the adding/removing of the underline by about 1000ms.
The cycling process should traverse twice through the list (1-2-3-4-5, 1-2-3-4-5) and then stop at 1.

<a href="#1">1</a> | <a href="#2">2</a>  | <a href="#3">3</a>  | <a href="#4">4</a>  | <a href="#5">5</a>  

Open in new window

0
Comment
Question by:aznprncritic
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 33678973
try this simple version first, and let me know if there is any prob
<style>
   nounderline 
   {
	text-decoration: none;
   }
</style>
<script>
	var anchors;
	var currentIndex = 0;
	var cycleNumber = 0;

   $(document).ready(function()
   {
	currentIndex = 0;
      	anchors = $("a");
      	cycleThoughOnce();
   });
   
   function removeUnderline(obj)
   {
         obj.addClass("nounderline ");
   }
   
   function addUnderline(obj)
   {
         obj.removeClass("nounderline ");
   }
   
   function cycleThough()
   {
   	if ( currentIndex == anchors.length )
   	{
   		if ( cycleNumber == 1)
   		{
   			return;
   		}
   		currentIndex = 0;
   		cycleNumber = 0;
   	}
   	if( cycleNumber == 0 )
   	{
	   	$(anchors[currentIndex++]).addClass("nounderline");
   	}
   	else
   	{
	   	$(anchors[currentIndex++]).removeClass("nounderline");
   	}
   	setTimeOut("cycleThough()", 100);
   }
  
  
</script>

<body>
<a href="#1">1</a> | <a href="#2">2</a>  | <a href="#3">3</a>  | <a href="#4">4</a>  | <a href="#5">5</a>  
</body>

Open in new window

0
 
LVL 22

Accepted Solution

by:
kadaba earned 2000 total points
ID: 33678993
I created a small plugin for this...
usage is very simple and you can see it in the attached code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>Loop It - Kadaba</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
	<script>
		(function($){
			$.fn.extend({ 
				loopThis: function(options) {

					var defaults = {
						toLoop:-1,
						loopDelay:1000
					};
					
					var options = $.extend(defaults, options);
				
					this.each(function() {
						
						var _self = this;
						var timer;
						var loopCount = 0;
						this._init = function(){
							timer = setTimeout(function(){ _self._loop(); },options.loopDelay);
						};						

						/**
						 * loop
						 */
						this._loop = function () {
							var shallLoop = options.toLoop == -1?true: (loopCount < (options.toLoop * $(this).find('a').length)?true:false);
							if(shallLoop)
							{
								var active = $(this).find('a.active');
								if ( active.length == 0 ) active = $(this).find('a:first');
								var next = active.next().length ? active.next(): $(this).find('a:first');
   								next.addClass('active');
								active.removeClass('active');
								loopCount++;
								timer = setTimeout(function(){ _self._loop(); },options.loopDelay);
							}
						};  
						
						this._init();	
					});
				}
			});
		})(jQuery);
	
	</script>
	
	<script type="text/javascript">
	$(document).ready(function() {
		$('#loopThisTwice').loopThis({toLoop:2});
		$('#loopThisMyWay').loopThis({toLoop:10,loopDelay:500});
		$('#loopThisForEver').loopThis();
	});	
	</script>
	<style>
	  .active{
		text-decoration:underline;
	  }
	  
	  a{
		text-decoration:none;
		color:black;
	  }
	</style>
</head>   
<body>

<b>This loops twice:</b>
<div id="loopThisTwice">
	<a href="#1" class="active">Link 1</a> | <a href="#2">Link 2</a>  | <a href="#3">Link 3</a>  | <a href="#4">Link 4</a>  | <a href="#5">Link 5</a>  
</div>

<br>
<b>This loops ten times with shorter duration of 500 ms:</b>
<div id="loopThisMyWay">
	<a href="#1" class="active">Link 1</a> | <a href="#2">Link 2</a>  | <a href="#3">Link 3</a>  | <a href="#4">Link 4</a>  | <a href="#5">Link 5</a>  
</div>

<br>
<b>This loops for ever:</b>
<div id="loopThisForEver">
	<a href="#1" class="active">Link 1</a> | <a href="#2">Link 2</a>  | <a href="#3">Link 3</a>  | <a href="#4">Link 4</a>  | <a href="#5">Link 5</a>  
</div>

</body>
</html>

Open in new window

0
 

Author Comment

by:aznprncritic
ID: 33684588
gurvinder372: your solution doesn't work at all.  You have misspellings in your code and even when corrected, it crashes my browser...

kadaba: very impressive, albeit slightly overkill.  would prefer this to be a simple function rather than a plugin.
0
 
LVL 22

Expert Comment

by:kadaba
ID: 33684669
Thanks.
:)

Here is the simplified version.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <title>Loop It - Kadaba</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
	<script type="text/javascript">
	$(document).ready(function() {
		timer = setTimeout(function(){ loop(); },1000);
	});	
	
	var loopCount = 0;
	var timer;
	function loop(){
		var shallLoop = (loopCount < (2 * $("#loopThisTwice").find('a').length)?true:false);
		if(shallLoop)
		{
			var active = $("#loopThisTwice").find('a.active');
			if ( active.length == 0 ) active = $("#loopThisTwice").find('a:first');
			var next = active.next().length ? active.next(): $("#loopThisTwice").find('a:first');
			next.addClass('active');
			active.removeClass('active');
			loopCount++;
			timer = setTimeout(function(){ loop(); },1000);
		}
	}
	</script>
	<style>
	  .active{
		text-decoration:underline;
	  }
	  
	  a{
		text-decoration:none;
		color:black;
	  }
	</style>
</head>   
<body>
<div id="loopThisTwice">
	<a href="#1" class="active">Link 1</a> | <a href="#2">Link 2</a>  | <a href="#3">Link 3</a>  | <a href="#4">Link 4</a>  | <a href="#5">Link 5</a>  
</div>
</body>
</html>

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question