本文介绍了done()到底是如何工作的,以及如何在done()中循环执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有这段代码.我试图在网页上一次显示一些内容,但是我相信我的代码存在的问题是done()在整个for循环完成之前什么也不会返回.

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. 我只想对URL进行一次ajax调用.
  2. 我想执行for循环,但是在每次迭代之后,我想返回一些东西(ajax?我不确定它叫什么,我仍然对Javascript还是陌生的.)

也许除了done()之外还有其他功能?

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

推荐答案

欢迎来到异步Java语言世界!一条建议:不要以从函数中返回的数据来思考,而是以每次准备好一些数据后要执行的函数来考虑.

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.

这篇关于done()到底是如何工作的,以及如何在done()中循环执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:54