.show()不工作里面ajaxstart()里面、工作、show、ajaxstart

2023-09-10 21:51:59 作者:青空如璃

下面是我的Java脚本加载图像时阿贾克斯被激发。

Below is my java script for loading image when ajax is fired.

<script type="text/javascript">
        jQuery(function (){
            $(document).ajaxStop(function() {
                $("#ajax_loader").hide();
                //alert("end");
                console.log(new Date().getTime());
            });
            $(document).ajaxStart(function() {
                $("#ajax_loader").show();
                //alert("start");
                console.log(new Date().getTime());
            });
        });
    </script>

下面

是内容,我隐藏/显示。

below is the content I'm hiding/showing.

<div id='ajax_loader'
            style="position: fixed; left: 50%; top: 40%; color: red; font-size: 3em; display : none; zIndex : 1;">
            processing.....
        </div>

问题是,当任何AJAX称为ajaxStart()方法被执行,但它没有显示的#ajax_loader的内容。

The problem is when any ajax is called the ajaxStart() method is executing but it is not showing the "#ajax_loader" content.

不过请求服用2〜3秒内做出回应。

Still the request is taking 2 to 3 secs to respond.

但是,如果我们取消注释提醒里面ajasStart()方法,则启用#ajax_loader的内容。

But if we uncomment alert inside ajasStart() method, then it is enabling the "#ajax_loader" content.

可以在任何请帮助我在这个问题上?

Can any please help me on this issue ?

推荐答案

这是不可能有一个负载指标和同步AJAX,不执行AJAX以异步的方式。

It is impossible to have a loading indicator and synchronous ajax, without executing the ajax in an asynchronous way.

这是因为浏览器渲染器被放入回调队列,队列回调仅被处理时调用堆栈是空的。同步AJAX $ P $从排空直到AJAX完成,因此,根本不可能引起任何呈现在页面上,直到后它是完整pvents调用堆栈

This is because the browser renderer gets put into the callback queue, and the callback queue only gets processed when the callstack is empty. Synchronous ajax prevents the callstack from emptying until the ajax is complete, thus making it impossible to cause anything to render on the page until after it is complete.

一个解决办法是包装在一个setTimeout的Ajax请求,从而使调用堆栈来清除,并呈现发生,Ajax请求被发送之前。但是,这不会与ajaxStart工作,因为这不会被调用,直到......你猜对了..阿贾克斯开始。这也有可能打破无论是要求你有这样的一个同步请求。

One workaround is to wrap the ajax request in a setTimeout, thus allowing the callstack to clear, and the renderer to happen, before the ajax request is sent. However, this will not work with ajaxStart because that doesn't get called until... you guessed it.. the ajax starts. This would likely also break whatever is requiring you to have this as a synchronous request.

唯一真正的解决方法是不使用同步Ajax和重构code,它是强加这个疯狂的要求你。

The only real solution to this is to not use synchronous ajax and refactor the code that is imposing this crazy requirement on you.

async: true