Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
enabling a timeout between each loop
Old 10-18-2011, 10:48 AM enabling a timeout between each loop
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Hi All

Im having a mare with something and wondered if someone could help me.

In brief, I am requiring to make 10 requests to a remote server to extract an object.

My problem is that my requests are occurring too quickly and i want to slow the process down.

I thought i could use a timer in my ajax call but for some reason, the timer runs only on the first ajax call which is defined in the each

so my code is

Code:
   
    $('ul#sortable2 li').each(function(index) {
    	
    	
    	var toFill = $(this);
    	 $.ajax({
   			 
  			  type: 'POST',
  			  url: "http://localhost/25-things-web/grooves/renderPlayer",
  			  async:true, 
  			  data: { list :  '27754583'  },
  			  
  			   success:function(data){	
  				  
  				   setTimeout( function() {  toFill.html(data);	 },10000);
  				   
  				  },
  				  error:function(){
  					 
  		       }
  		 } ); 
    });
each li should pull the object and render it but i want to be able to define a time between each ajax call and render the results in between

My timeout runs at the start of the each but not after each each.

Any ideas
this is really a pain and a probl;em for me.


Cheers

Steve
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 10-18-2011, 02:41 PM Re: enabling a timeout between each loop
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Isn't there a delay method?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 10-18-2011, 02:59 PM Re: enabling a timeout between each loop
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
well i thought i had sorted the delay by the below

setTimeout( function() { toFill.html(data); },10000);

But the delay comes at the start or seems to and then everythign loads in one hit
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 10-18-2011, 03:19 PM Re: enabling a timeout between each loop
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Maybe you can add a counter that increases for every element in the "each" call, which you also multiply the delay by. Give it a go :P
Code:
var i = 0;
$('ul#sortable2 li').each(function(index) {
        var toFill = $(this);
        $.ajax({
                type: 'POST',
                url: "http://localhost/25-things-web/grooves/renderPlayer",
                async:true, 
                data: { list :  '27754583'  },
                success:function(data){	
                        setTimeout(function() { toFill.html(data); }, 10000 * i++);
                },
                error:function(){
                }
        } ); 
});
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 10-18-2011, 03:22 PM Re: enabling a timeout between each loop
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
yur will give that a go and post back here.
cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 10-18-2011, 03:28 PM Re: enabling a timeout between each loop
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
actually looking at this it looks like quite a good idea.
Cant sort now till am but will get back to you on this.

cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 10-20-2011, 04:14 AM Re: enabling a timeout between each loop
Novice Talker

Posts: 10
Name: Umar Bukhari
Location: Pakistan
Trades: 0
This may help you out

Code:
$('li').each(function(indexInArray){
   var data = $(this).text();
   setTimeout( function () {
       requestFunction(data, function(status){
           if ( status == 'OK' ) do stuff...
       });
   }, indexInArray * 500);
});
stackoverflow.com/questions/7445941/how-to-make-delay-between-each-loops-of-jquery-each-function
angelsdev is offline
Reply With Quote
View Public Profile Visit angelsdev's homepage!
 
Old 10-23-2011, 09:08 AM Re: enabling a timeout between each loop
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You could of course just use a Date object.

Code:
function pause(mS) 
{
        var date = new Date();
        var curDate = null;

        do { curDate = new Date(); } 
        while(curDate-date < mS)
}
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 10-23-2011 at 09:09 AM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to enabling a timeout between each loop
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.59968 seconds with 12 queries