简体   繁体   English

通过Ajax发送请求时如何停止挂页

[英]How can i stop hanging page when a request is send via Ajax

I am facing a serious issue... Whenever i use Ajax to send a request and get an response my browser got hanged.. and show no loading etc... 我面临着一个严重的问题...每当我使用Ajax发送请求并获得响应时,我的浏览器就被挂起了..并且没有显示加载等...

But when i response is retrieved from the Ajax then browser and page again start working... 但是当我从Ajax检索到响应时,浏览器和页面再次开始工作...

Below is the code that i used..... 下面是我使用的代码.....

function ShowContestStatus(contestID)
{
    $("#showContestDetails").html('<div class="loadercontest"><img src="assets/images/loading.gif">Loading Contest....</div>');

    $("#RadioGroup1_0, #RadioGroup1_1, #RadioGroup1_2").prop('disabled', true);
    $.ajax({
            url:"process/processMyContest.php",
            type:'POST',
            cache:false,
            async:false,
            data : {act : 'showcontest', cid : contestID },
            success:function(result)
            { 
                $("#showContestDetails").html(result);
                $("#RadioGroup1_0, #RadioGroup1_1, #RadioGroup1_2").prop('disabled', false);
            }
        });
    }

Please help me on this... i want to get the same response as on other websites when you send a request and they are using ajax the page neither hanged and also each processing like scrolling etc is visible ...... 请对此提供帮助...当您发送请求时,我希望得到与其他网站相同的响应,并且他们使用ajax时,该页面既没有挂起,也可以看到诸如滚动等的每个处理……

So please suggest me good ideas.... so i can get rid of it and make my ajax smooth for page without effecting and irritate the other person by hanged... 所以请给我建议好主意...。这样我就可以摆脱它,使我的ajax在页面上保持平滑,而不会影响和绞死其他人...

Thanks in advance...:) 提前致谢...:)

The problem is async:false ... Since your ajax request is synchronous the script execution will wait for the request to complete to continue.. 问题是async:false ...由于您的ajax请求是同步的,脚本执行将等待请求完成才能继续。

Since browser uses a single threaded execution pattern(either it will execute script or repaint or wait for user events at a time- not all at the same time), your browser tab will stop listening to user(so it will look like it is hanged) 由于浏览器使用单线程执行模式(它将执行脚本或重新绘制,或者一次等待用户事件-并非同时执行),因此您的浏览器标签将停止监听用户(因此看起来好像已被挂起) )

function ShowContestStatus(contestID) {
    $("#showContestDetails").html('<div class="loadercontest"><img src="assets/images/loading.gif">Loading Contest....</div>');

    $("#RadioGroup1_0, #RadioGroup1_1, #RadioGroup1_2").prop('disabled', true);
    $.ajax({
        url: "process/processMyContest.php",
        type: 'POST',
        cache: false,
        //remove async: false,
        data: {
            act: 'showcontest',
            cid: contestID
        },
        success: function (result) {
            $("#showContestDetails").html(result);
            $("#RadioGroup1_0, #RadioGroup1_1, #RadioGroup1_2").prop('disabled', false);
        }
    });
}

Ajax.async Ajax.async

By default, all requests are sent asynchronously (ie this is set to true by default). 默认情况下,所有请求都是异步发送的(即默认情况下设置为true)。 If you need synchronous requests, set this option to false. 如果需要同步请求,请将此选项设置为false。 Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. 跨域请求和dataType:“ jsonp”请求不支持同步操作。 Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active . 请注意,同步请求可能会暂时锁定浏览器,从而在请求处于活动状态时禁用任何操作 As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; 从jQuery 1.8开始,不建议使用async:false和jqXHR($ .Deferred); you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success(). 您必须使用成功/错误/完成回调选项,而不要使用jqXHR对象的相应方法,例如jqXHR.done()或已弃用的jqXHR.success()。

将async:true设为使浏览器在运行ajax代码时侦听其他事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM