You can call setTimeout again and keep track of your loop counter, or you
can use setInterval.

To make that easy, I posted a slowEach() plugin to this group a while back.
It may be just what you need. Here's the original post:

http://groups.google.com/groups/search?q=sloweach+jquery+plugin

Here's an updated version of that code that supports both ways of calling
it:

    $.slowEach( array, interval, callback );

Or:

    $( array-or-selector-etc ).slowEach( interval, callback );

The plugin code is:

    jQuery.slowEach = function( array, interval, callback ) {
        if( ! array.length ) return;
        var i = 0;
        next();
        function next() {
            if( callback.call( array[i], i, array[i] ) !== false )
                if( ++i < array.length )
                    setTimeout( next, interval );
        }
    };

    jQuery.fn.slowEach = function( interval, callback ) {
        jQuery.slowEach( this, interval, callback );
    };

These functions work just like $.each() (for an array only, not an object),
or $(...).each(), except that they inserts a delay between each iteration of
the loop.

For your use case, you could do something like this:

    var myURL = "getSearchResults.php";
    $.getJSON( myURL, function( data ) {
        $.slowEach( data, 1000, function( i, value ) {
            $("#searchResults").html( value );
        });
    });

Or you could write it this way if you prefer:

    var myURL = "getSearchResults.php";
    $.getJSON( myURL, function( data ) {
        $(data).slowEach( 1000, function( i, value ) {
            $("#searchResults").html( value );
        });
    });

If you want to stop the loop before it's finished, return false from the
callback function (where the .html() call is made).

-Mike

> From: bwdev
> 
> Yes but the set TImeout seems to happen once.  I need it to 
> pause on each interation of the loop.
> 
> On Dec 30, 1:05 pm, "Michael Geary" <m...@mg.to> wrote:
> > Not only will that lock up the current browser, in many browsers it 
> > will lock up *every* browser window or tab. Plus, if someone is 
> > running on battery power it will drain the battery needlessly.
> >
> > As Richard said, setTimeout is the right solution:
> >
> >     doSomething();
> >     setTimeout( function() {
> >         doSomethingLater();
> >     }, 5000 );
> >
> > -Mike
> >
> > > From: Joe
> >
> > > // Similar to PHP's sleep function.
> > > function pause(numOfSeconds) {
> >
> > >     numOfSeconds *= 1000;
> > >     var startTime = new Date().getTime();
> > >     while (new Date().getTime() < startTime + numOfSeconds);
> > >     return false;
> > > }
> >
> > > // Let's pause for 5 seconds...
> > > pause(5);
> > > console.log('Awake Now');
> >
> > > A simple way of doing it, but it will lock the browser...
> >
> > > Cheers.
> >
> > > Joe
> >
> > >http://www.subprint.com
> >
> > > On Dec 30, 10:23 am, bwdev <pel...@gmail.com> wrote:
> > > > I have the following code and want to basically write
> > > something out,
> > > > let it sit for a while, and then write something else
> > > out...how do I
> > > > do the pause/wait?
> >
> > > > Thanks!
> >
> > > > myURL="getSearchResults.php";
> > > >                                 $.getJSON(myURL, 
> function(data) {
> > > >                                 for (var i=0; i < data.length; 
> > > > i++)
> >
> > > >                                        
> > > > $("#searchResults").html(data[i]);
> > > >                                         //I want to 
> pause here....
> >
> > > >                                         });
> 

Reply via email to