'How to stop Looping on javascript after several times
I am trying to make disappear loop using javascript, the codes running well.. but i want to make the loop stop after several times.. this is the codes :
$(document).ready(function() {
var j = 0;
var delay = 9000; //millisecond delay between cycles
function cycleThru(){
var jmax = $("ul#cyclelist li").length -1;
$("ul#cyclelist li:eq(" + j + ")")
.animate({"opacity" : "1"} ,400)
.animate({"opacity" : "1"}, delay)
.animate({"opacity" : "0"}, 400, function(){
(j == jmax) ? j=0 : j++;
cycleThru();
});
};
cycleThru();
});
What should i do if i want to stop the loop after 10x?
Thanks for the help
Solution 1:[1]
Take this line var jmax = $("ul#cyclelist li").length -1; outside the function. Else every time cycleThru is called it will create new j
$(document).ready(function() {
var jmax = $("ul#cyclelist li").length - 1;
var j = 0;
var delay = 9000; //millisecond delay between cycles
function cycleThru() {
$("ul#cyclelist li:eq(" + j + ")")
.animate({
"opacity": "1"
}, 400)
.animate({
"opacity": "1"
}, delay)
.animate({
"opacity": "0"
}, 400, function() {
if (j !== max) {
j++;
cycleThru();
}
});
};
cycleThru();
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | brk |
