简体   繁体   English

设置要执行的功能的顺序

[英]Set order of functions to execute

I have two Jquery function. 我有两个Jquery函数。 How do I set the execution order so that function B would only be called after function A, (reason behind is that Function A set a value to a variable IdstoExclude that is getting passed as a parameter to function B. Below is what i tried but no luck: 我如何设置执行顺序,以便仅在函数A之后调用函数B,(原因是函数A为变量IdstoExclude设置了一个值,该值作为参数传递给函数B。下面是我尝试过的操作,但没运气:

var IDstoExclude = "123";
callListService('getArticleTypelistById', 'Atid', 87, 5, '#MainStory', '#tmplFeaturePanel', IDstoExclude);
callListService('getArticleTypelistById', 'Atid', 87, 10, '#LeftSideContent1', '#tmplLeftSideContent1', IDstoExclude);


function callListService(webServiceName, parameterName, parameterValue, noOfItems, domElement, templName, exclIDs) {
    //set a default value for the template name * exclIDs
    templName = templName || "#FeaturedSubStories";
    //exclIDs = exclIDs || "123,12";
    var inputParameters = webServiceName.toLowerCase() + ':' + parameterName.toLowerCase() + ':' + parameterValue + ':noofitems:' + noOfItems + ':excludeids:' + exclIDs;
    var clientcode = getCryptoToken(inputParameters);
    //Build JSONp query
    eval("data={" + parameterName.toLowerCase() + ":" + parameterValue + ", noofitems: " + noOfItems + ", excludeids:" + exclIDs + ",  clientcode:'" + clientcode + "' }");

    $.getJSON('https://abc.com/Service.svc/' + webServiceName + '?callback=?', data, function (data2) {
        var template = $.templates(templName);
        var htmlOutput = template.render(data2);
        $(domElement).append(htmlOutput);
        IDstoExclude = data2.IdsInThisList;

    });

Tried below but no luck: var IDstoExclude = "123"; 在下面尝试过但没有运气:var IDstoExclude =“ 123”;

function callService1() {
    return $.ajax()
        .then(function(response) {
            callListService('getArticleTypelistById', 'Atid', 87, 10, '#LeftSideContent1', '#tmplLeftSideContent1', IDstoExclude);
        });
}

function callService2() {
    callListService('getArticleTypelistById', 'Atid', 87, 10, '#LeftSideContent1', '#tmplLeftSideContent1', IDstoExclude)
}

$.when(callService1()).then(callService2);

For this solution to work as you expect your code should look like something below: 为了使此解决方案按预期工作,您的代码应类似于以下内容:

function callService1(p1, p2, ...) {
    return $.ajax(...)
        .then(function(response) {
            return something;
        });
}

function callService2(something_from_s1) {
    // work with something_from_s1 var
}

$.when(callService1(...)).then(callService2);

References: 参考文献:

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

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