EdwardBlack EdwardBlack - 3 months ago 11
jQuery Question

using setTimeout in each loop does not work

I have a huge galerie with about 400 pictures. I made a button so i can delete all pictures from the DOM and also from the server (by using an ajax request for each file).

I tried to use

setTimeout
inside my for each loop to give all items a background-color of red, just for testing. However nothing happens if i click my button!

//run trough all pages
pages.each
(
function()
{
var items = $(this).children("ul").children("li");

//run through each item of the page
items.each
(
function()
{
setTimeout(function() { $(this).css("background-color","red"); }, 100);
}
);
}
);


If i run this code without setTimeout, then it works. I also tried other waiting times. No errors are thrown.

Answer

this should work

setTimeout(function(el) { el.css("background-color","red"); }, 100, $(this));