while循环与jQuery异步AJAX调用 [英] While loop with jQuery async AJAX calls

查看:1109
本文介绍了while循环与jQuery异步AJAX调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事情:
我有一个页面,它必须显示不确定的图像数量,通过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.

推荐答案

您最好的选择是重组代码以使用异步当第一个完成时,ajax调用并启动下一个调用,等等。这将允许页面在图像提取之间重新显示。

Your best bet would be to restructure your code to use async ajax calls and launch the next call when the first one completes and so on. This will allow the page to redisplay between image fetches.

这也将给浏览器一个呼吸和照顾其他家务的机会,而不是认为这可能被锁定或者挂起。

This will also give the browser a chance to breathe and take care of its other housekeeping and not think that maybe it's locked up or hung.

而且,使用 async:'false'是一个坏主意。我没有理由为什么正确结构化的代码不能在这里使用异步的ajax调用,而不是在您获取这些数据时挂起浏览器。

And, use async: 'false' is a bad idea. I see no reason why properly structured code couldn't use asynchronous ajax calls here and not hang the browser while you're fetching this data.

您可以使用异步ajax这样:

You could do it with asynchronous ajax like this:

function getAllImages(position, maxImages) {
    var imgCount = 0;

    function getNextImage() {
        $.ajax({
            url: urlAJAX + 'scan=' + position,
            method: 'GET',
            async: true,
            success: function(data) {
                if (data.status == "success" && imgCount <= maxImages) {
                    ++imgCount;
                    renderImageData(data);
                    getNextImage();
                }
            }
        });
    }
    getNextImage();
}

// no while loop is needed
// just call getAllImages() and pass it the 
// position and the maxImages you want to retrieve
getAllImages('front', 20);

此外,虽然这可能看起来像递归,但它并不是真的递归,因为异步性质ajax电话。 getNextImage()在下一个调用之前已经完成,因此在技术上不会递归。

Also, while this may look like recursion, it isn't really recursion because of the async nature of the ajax call. getNextImage() has actually completed before the next one is called so it isn't technically recursion.

这篇关于while循环与jQuery异步AJAX调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆