I am trying to highlight each menu items after 1 sec gap.
It works fine, but I am facing a problem for the last menu item. I added
$(strtemp).removeClass("change");
for clearing the last menu item, but that line is getting executed before the setInterval
function gets executed. I am not able to understand why?
my.js
$(document).ready(function(){
$(".parent").click(function(){
str = "li:nth-child(";
strtemp="";
i=1;
var refreshID=setInterval(function(){
str1=str.concat(i);
str1=str1.concat(")");
str2=str.concat(i-1);
str2=str2.concat(")");
if($(str2).hasClass("change")){
$(str2).removeClass("change");
}
$(str1).addClass( "change" );
if(i==10){
strtemp=str1;clearInterval(refreshID);
}
i++;
}, 1000);
$(strtemp).removeClass("change");
});
});
index.html
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<script type="text/Javascript" src="jquery.js"></script>
<script src="my.js"></script>
<title>My Jquery</title>
</head>
<body>
<div class="parent">
<ol>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
<li>AAAAA</li>
</ol>
</div>
</body>
</html>
stylesheet.css
.parent{
background-color: yellow;
height:200px;
width:400px;
}
.change{
border: 1px dotted green;
background-color: red;
height:15px;
width:100px;
}