This JQuery function should timeout to ensure that each item is delayed 8 seconds after the last. Thus producing a gallery where images fade in and out 8 seconds apart from each other.
It doesn't work.
Any ideas.
function gallery() {
var timeout = 0;
$('.cornerimg').each(function() {
setTimeout(function() {
$(this).addClass('cornerimgfocus');
setTimeout(function() {
$(this).removeClass('cornerimgfocus');
timeout += 8000;
}, (timeout + 8000));
},timeout);
});
}
Marvellous
In your setTimeout calls, 'this' is not equal to the element you think it is, it's equal to the DOMWindow. Try this version instead as I found it to be simpler.
You probably want to use recursion and some kind of index.
Create a function that remove the focus from the previous image (or perhaps just all the images if that will work) and then puts the cornerimgClass on the one that matches an index supplied to the function. Then once this is done it uses setTimeout to call itself agin in 8 seconds with the index incremented by one.
You'll want to do a check for when you reach the end of the list and either stop, reset to 0 or whatever you fancy.
The key thing though is to use recursion with named functions rather than just anonymous functions.
setTimeout(func, 0)
does not immediately execute the function, only after the current script finished, sotimeout
only gets incremented aftersetTimeout
has been called for all the corners (with identical delay). Try it like this: