0
   $.ajax({
        url: '/api/1.0/tweets.php?username=' + username
    }).done(function (data) {
        //For loop here to do something multiple times
    )};

I have this piece of code. I'm trying to get something on my web page to display one at a time, but I believe the problem with my code is that done() is causing nothing to return until the entire for loop is finished.

  1. I want to just do the ajax call to the url once.
  2. I want to execute the for loop, but after each iteration, I want to return something (the ajax? I'm not sure what it's called, I'm still new to this Javascript stuff).

Maybe there's another function other than done()?

dtgee
  • 1,272
  • 2
  • 15
  • 30
  • *done() is causing nothing to return until the entire for loop is finished* => No, the `for` loop is causing execution to not proceed until the `for` loop is finished. It's kind of what loops do. The question is meaningless without the loop and more detail (you want to return *something*?) – Jon Aug 16 '13 at 20:34
  • you can only return from a scope once. also, returning from the done callback doesn't do anything useful. – Kevin B Aug 16 '13 at 20:35
  • Gah I'm kind of stumped. So I have to edit my for loop then? Maybe make it a recursive call? – dtgee Aug 16 '13 at 20:38
  • You say "to display one at a time". Maybe something like: http://jsfiddle.net/TkEVw/1/ – Ian Aug 16 '13 at 20:48

1 Answers1

4

Welcome to the world of asynchronous Javascript! One piece of advice: instead of thinking in terms of data being returned from functions, think in terms of functions that are executed each time some piece of data is ready.

What you need is something that will call your callback each time a piece of data is available. A similar question was asked here, with several answers that you may find helpful.

Community
  • 1
  • 1
Jonathan Warden
  • 2,492
  • 1
  • 17
  • 11