Solved

Simple Javascript Loop

Posted on 2010-09-14
4
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Expert Comment

by:gurvinder372
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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

617 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