Jump to content

SetTimeout inside .each with ajax


Recommended Posts

The same old story.

$('li').each(function(){ setTimeout(function() {  // ajaxCall(..... },2000);});

Is it posible at all?


I dont what to heat up my room with at empty loop with Date=Date argument

Link to comment
Share on other sites

It's possible to put a setTimeout in an each() function.


What happens when you run that is that instantly there will be as many setTimeout() calls as there are <li> elements on the page, they all will fire at the same time exactly two seconds after .each() was called.


What are you trying to achieve with this?

Link to comment
Share on other sites

I'm geting array of page urls from ul list.

Then i'll have to check variables on those pages. Maybe send few other ajax requests.

This should be done on JS.

Ajax "success" isnt good idea becaue ajax functions are used from other libraries.

So easiest solution is setTimeout.

Link to comment
Share on other sites

By putting the AJAX request inside the setTimeout() the request does not begin until two seconds, after that it could take any amount of time to get a response from the server. It could even take four or ten seconds.


You haven't fully described the problem to me yet, you just are telling me what tools you tried to use, you might be using the wrong tools.


If I understood correctly, you have a <ul> element on the page and in each list item there is a URL you want to send an AJAX request to. Your current jQuery selector is loooking at absolutely every <li> element on the page, even ones that don't belong to the list, so you should give your list an ID so that other lists on the page don't interfere: $('ul#urls li')


Responses (in jQuery) are dealt with in the success function, there is no other way. Can you explain specifically what doesn't work when you use it?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...