2

我正在尝试制作一个足够基本的页面,允许用户通过单击按钮来执行 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>          
4

2 回答 2

1

我会实施一个计数器。每次显示加载指示器时,将计数器加一,每次要隐藏它时,减一。然后监视计数器,当它高于零时显示加载指示器,当为零时隐藏它。说得通?

于 2013-05-17T22:25:04.317 回答
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
};
于 2013-05-17T22:50:26.027 回答