I am trying to add and remove a class within a <span>, but the removeClass is not working though the function is being called.
setInterval(changeClass,4000);
var spanId=1;
function changeClass(){
$('#'+spanId).addClass("hilite");
setTimeout(remove, 1000);
spanId++;}
function remove(){
$('#'+spanId).removeClass("hilite");
return true;
}
Can any one tell the reason ?
How can I do this with a while loop something like this i tried all my ways but chould not get it working
var spanSet=4;
var spanId=1;
while(spanSet > 0)
{
changeClass();
spanSet--;
}
function changeClass(){
$('#'+spanId).addClass("hilite");
setTimeout(remove, 3000);
}
function remove(){
$('#'+spanId).removeClass("hilite");
spanId++;
return true;
}
In this code, you should execute
spanId++in a callback function which is passed as parameter to setTimeout since JS won’t halt the execution until it finishes setTimeout statement.So your code should look like this: