更新时间:2022-06-24 22:36:46
不同步做。使用回调。这里是一个演示给你: http://jsfiddle.net/s368a/1/
Don't do it synchronously. Use the callback. Here is a demo for you: http://jsfiddle.net/s368a/1/
<ul class="quoteList"></ul>
<input type="button" onclick="getData();" value="Go Get It!">
<script>
var counter = 0;
function getData()
{
$.ajax({
url:'http://whisperingforest.org/js/getQuote.php',
async: true,
dataType: 'jsonp',
success:function(data){
$('.quoteList').append('<li>' + data +'</li>');
counter++;
if (counter < 5) getData();
}
});
}
</script>
设置异步
假块主线程(负责执行的JavaScript,呈现在屏幕等),并等待XHR完成。
Setting async
to false blocks the main thread (responsible for executing JavaScript, rendering the screen, etc) and waits for the XHR to complete.
这是几乎总是一个可怕的想法。用户不喜欢没有响应的用户界面。 (http://***.com/a/20209180/3112803)
This is almost always a terrible idea. Users don't like unresponsive UIs. (http://***.com/a/20209180/3112803)
只要搜索计算器的 AJAX异步:假
,你会发现这么多很好的解释。每个人都和他们的兄弟与鼓励你使用异步:假
。这里是一个很好的解释:http://***.com/a/14220323/3112803
Just search *** for ajax async: false
and you will find MANY good explanation on this. Everyone and their brother with discourage you from using async:false
. Here's is a great explanation: http://***.com/a/14220323/3112803