Javascript / AJAX异步加载旋转器

时间:2013-05-17 22:18:07

标签: javascript jquery ajax spinner loading

我正在尝试创建一个足够基本的页面,允许用户通过单击按钮来执行php脚本。单击时,每个按钮都会有一个加载微调器弹出窗口。

我的问题是,在点击一个按钮然后点击另一个按钮时,两个微调器都会在同一时间关闭,即使第二个可能仍在处理。

有谁知道如何使这些微调器真正异步?非常感谢,它杀了我。

JS:

function test(element){
    var append = "#";
    var test = append.concat(element);

    document.getElementById(element).style.visibility='visible';

            $.ajax({url:"test.php",success:function(result){
                    hide(element);
            }

            });
         };

    function hide(element){
        document.getElementById(element).style.visibility='hidden';
    };


</script>   

HTML:

 <html>
    <?
    $index = 0;
$myArray = array ("1", "2", "3", "4", "5");
for($index = 0; $index < 5; $index++){?>

    <button   onclick="test('<?echo $myArray [$index];?>')">Start</button>

<img id="<?echo $myArray [$index];?>" src="images/loader.gif"        
     style="visibility:hidden"/>
    <br><br>

    <?}?>

   </html>          

2 个答案:

答案 0 :(得分:1)

我会实施一个计数器。每次显示加载指示器时,向计数器添加一个,每次要隐藏它时,减去一个。然后监控计数器,当它高于零时显示加载指示器,当零时隐藏它。有意义吗?

答案 1 :(得分:1)

类似下面的(未经测试的)代码可能会起作用,它巧妙地意味着你可以避免在ajax请求中担心微调器:

var spinningAjax = (function() { // use of the closure created by an immediate function gives us the scope to create a persistant counter variable
    var counter = 0;
    $(document).ajaxComplete(function() {
        counter--;
        if (counter === 0) {
            showSpinner(false);
        }
    });
    return function(settings) {
        counter++;
        showSpinner(true);
        $.ajax(settings);
    }
})();

var showSpinner(bool) {
    // I'll leave this up to you as it looks like your posted html / js is for example purposes rather than replicating your actual site
};

编辑:好的,看到其他答案的评论,我意识到这并不能解决你所处的情况。我会思考,看看能不能做得更好

EDIT2:我认为这个(仍然未经测试,很遗憾)代码可能就是您所需要的。如果您有任何问题,请在评论中告诉我。

var spinningAjax = (function() {  // closure of immediate function lets us create a persistant array of the counters for each spinner
    var counter = [];  // an array to hold the counters for each spinner
    $(document).ajaxComplete(function(event, xhr, settings) { // called whenever any ajax request is completed
        if (typeof settings.ajaxGroup !== 'undefined') { // only update the counters if an ajaxGroup has been provided
            counter[settings.ajaxGroup]--;
            if (counter[settings.ajaxGroup] === 0) {
                showSpinner(false, settings.ajaxGroup); // hide spinner when all requests connected with the spinner have been completed
            }
        }
    });
    return function(settings) { // this is the function actually assigned to the variable spinningAjax as a result of the immediate function
        counter[settings.ajaxGroup] = counter[settings.ajaxGroup] ? counter[settings.ajaxGroup]+1 : 1; // can't just use the ++ operator as this property might not be defined yet
        showSpinner(true, settings.ajaxGroup);
        $.ajax(settings);
    }
})();

var showSpinner(bool, spinnerIdentifier) {
    // I'll leave this up to you as it looks like your posted html / js is for example purposes rather than replicating your actual site
};