$ .holdReady对$(document).ready()没有影响

时间:2014-05-08 10:31:00

标签: jquery

如何使用holdReady以及doc准备好了?我需要确保在doc.ready之前holdReady被解雇,我正在尝试下面,但由于loadTab3()之前storeContentList仍然被解雇而没有运气?

$.holdReady(true);

tab3Data = storeContentList('getArticleTypelistById', 'atid', '88', '5', '123');

$.holdReady(false);

$(document).ready(function () {
   //  tab3Data gets used in loadTab3 function below.
   loadTab3();
});


 function storeContentList(webServiceName, parameterName, parameterValue, noOfItems, IDstoExclude) {
        var inputParameters = webServiceName.toLowerCase() + ':' + parameterName.toLowerCase() + ':' + parameterValue + ':noofitems:' + noOfItems + ':excludeids:' + IDstoExclude;
        var clientcode = getCryptoToken(inputParameters);
        eval("data={" + parameterName.toLowerCase() + ":" + parameterValue + ", noofitems:" + noOfItems + ", excludeids:'" + IDstoExclude + "',clientcode:'" + clientcode + "' }");
        $.ajax({
            url: 'https://abc/Service.svc/' + webServiceName,
            dataType: 'jsonp',
            async: false,
            data: data,
            success: function (data) {
                //tab3Data = JSON.stringify(data);
                tab3Data = data;
                console.log('tab3Data in storeContentList function:' + tab3Data);
                return data;
            },
            error: function (data) {
                console.log('err occured');
            }
        });
    }

1 个答案:

答案 0 :(得分:0)

不要将async: false用作AJAX选项。

......永远不会。永远。同步AJAX请求很糟糕。他们锁定了浏览器。几乎没有理由使用它们。不惜一切代价避免它们。我不能强调这一点。


我详细讨论here,但 TL; DR 是JSONP作为传输,不支持同步请求。

理想的解决方案是完全取消$.holdReady,并采用AJAX的异步特性。基本上,您的async: false选项被忽略,并且AJAX请求是异步发生的。

$(document).ready(function () {
    storeContentList();
});


 function storeContentList(webServiceName, parameterName, parameterValue, noOfItems, IDstoExclude) {
        var inputParameters = webServiceName.toLowerCase() + ':' + parameterName.toLowerCase() + ':' + parameterValue + ':noofitems:' + noOfItems + ':excludeids:' + IDstoExclude;
        var clientcode = getCryptoToken(inputParameters);
        eval("data={" + parameterName.toLowerCase() + ":" + parameterValue + ", noofitems:" + noOfItems + ", excludeids:'" + IDstoExclude + "',clientcode:'" + clientcode + "' }");
        $.ajax({
            url: 'https://abc/Service.svc/' + webServiceName,
            dataType: 'jsonp',
            data: data,
            success: function (data) {
                loadTab3(data); // Now pass data as an parameter to `loadTab3`; modify your code accordingly.
            },
            error: function (data) {
                console.log('err occured');
            }
        });
    }

然后将loadTab3的声明从function loadTab3()更改为function loadTab3(tab3Data)

但是,由于您需要在变量中保留响应,因此最简单(并且对于应用程序其余部分最透明的方式)执行此操作将继续使用$.holdReady,如此;

$.holdReady(true);

var tab3Data;

storeContentList('getArticleTypelistById', 'atid', '88', '5', '123');

$(document).ready(function () {
   //  tab3Data gets used in loadTab3 function below.
   loadTab3();
});


 function storeContentList(webServiceName, parameterName, parameterValue, noOfItems, IDstoExclude) {
        var inputParameters = webServiceName.toLowerCase() + ':' + parameterName.toLowerCase() + ':' + parameterValue + ':noofitems:' + noOfItems + ':excludeids:' + IDstoExclude;
        var clientcode = getCryptoToken(inputParameters);
        eval("data={" + parameterName.toLowerCase() + ":" + parameterValue + ", noofitems:" + noOfItems + ", excludeids:'" + IDstoExclude + "',clientcode:'" + clientcode + "' }");
        $.ajax({
            url: 'https://abc/Service.svc/' + webServiceName,
            dataType: 'jsonp',
            data: data,
            success: function (data) {
                tab3Data = data;

                $.holdReady(false);
            },
            error: function (data) {
                console.log('err occured');
            }
        });
    }