While循环使用jQuery AJAX异步调用 [英] While loop with jQuery async AJAX calls
问题描述
的事情: 我有一个页面,其中有显示未定数量的图像,通过AJAX加载(使用base64编码的服务器端),一个接一个。
The thing: I have a page, which has to display undetermined number of images, loaded through AJAX (using base64 encoding on the server-side) one by one.
var position = 'front';
while(GLOB_PROCEED_FETCH)
{
getImageRequest(position);
}
function getImageRequest(position)
{
GLOB_IMG_CURR++;
$.ajax({
url: urlAJAX + 'scan=' + position,
method: 'GET',
async: false,
success: function(data) {
if ((data.status == 'empty') || (GLOB_IMG_CURR > GLOB_IMG_MAX))
{
GLOB_PROCEED_FETCH = false;
return true;
}
else if (data.status == 'success')
{
renderImageData(data);
}
}
});
}
的问题是,图像(与renderImageData()函数构造)被附加(一起),以一定的DIV,只有当所有的图像被提取。我的意思是,没有任何DOM操作可能直到循环结束。
The problem is that images (constructed with the renderImageData() function) are appended (all together) to the certain DIV only when all images are fetched. I mean, there is no any DOM manipulation possible until the loop is over.
我需要加载并之一,因为图像可能数量庞大的显示图像,所以我不能堆叠起来,直到他们都将被读取。
I need to load and display images one by one because of possible huge number of images, so I can't stack them until they all will be fetched.
推荐答案
尝试使用的setInterval()
函数代替,而()
。
var fetch = setInterval(loadImage, 2000);
function loadImage(){
position= new position; //Change variable position here.
getImageRequest(position);
if(!GLOB_PROCEED_FETCH){
clearInterval(fetch);
}
}
这篇关于While循环使用jQuery AJAX异步调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!