Consider a repeating loop of
setInterval(function(){
$('.test').each(function(idx){
var duration = 1000;
$(this).delay(duration*idx);
Some stuff here
});
}, 4000);
How can I calculate the total time spent for the internal loop to set the setInterval dynamically?
Here, if we have 4 elements with class of test, the cycle works perfectly with setInterval of 4000. How can I make a variable to set this value dynamically? For example, setting it 6000, when we have 6 elements, and so forth.
My problem is that, I cannot use the value of duration*idx for setInterval, as it is inside the idx function.
You can use .length to count the number of elements:
And then, perform your actions!
EDITED, to clarify the use of .lenght over .size():
The .size() method is functionally equivalent to the .length property; however, the .length property is preferred because it does not have the overhead of a function call.