• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Simple Javascript Loop

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
aznprncritic
Asked:
aznprncritic
  • 2
1 Solution
 
Gurvinder Pal SinghCommented:
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
 
kadabaCommented:
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
 
aznprncriticAuthor Commented:
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
 
kadabaCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now