首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >jquery "when + Then“未按预期工作

jquery "when + Then“未按预期工作
EN

Stack Overflow用户
提问于 2017-03-16 22:53:24
回答 1查看 113关注 0票数 0

我使用jquery的"when,Then“来做一些操作。我正在使用doAddProcedure函数执行一些计算。我期待一个结果,比如在执行doAddProcedure函数中的代码后,控制将返回到AddProcedures,然后返回到完成部分.But中的代码,它没有按预期工作。此外,我还展示了一个加载器,以显示在doAddProcedure部分的代码执行时间。在doAddProcedure中执行代码所用的时间内,加载器未显示。请帮我修一下我的英语issue.Sorry

这是我的代码

代码语言:javascript
运行
复制
    var tot_codes = 0;   
    function doAddProcedure(thisval)
    {

        top.ShowAjaxLoader('Loading..');     
        var countval = $("#last_id").val();


          //My code block....
         return true;
    }  
    /**
     * Function to add procedures
     * @returns {undefined}
     */ 
    function AddProcedures(thisval)
    {
        $.when(doAddProcedure(thisval)).then(function(){        
          if (tot_codes > 0) {
              //setTimeout(function(){ 

                top.notification('successfully added the codes.');            
                //top.window.parent.document.getElementById('circularG').hide();
                window.parent.phFrntpayClosePopup();           
                //top.window.parent.document.getElementById("loaderHtml").style.display = "none";          
              //}, 3000); 

        } else {          
            top.notification('Please select atleast one code.');     
        }           
        }); 
    }

AddProcedures(thisval); // Calling main Function 
EN

回答 1

Stack Overflow用户

发布于 2017-03-16 23:07:55

这是因为,当您没有向when传递延迟或承诺时,then将立即执行。查看When..Then的jquery文档

在您的示例中,您将when()视为等待true的某种if条件……我建议你阅读更多关于PromiseDeferred的知识,然后回到when..then逻辑或使用Promise。

当然,你也可以像下面这样使用回调函数:

代码语言:javascript
运行
复制
doAddProcedure(thisVal,callbackFunc){
// do stuff
callbackFunc();
// If you wish to wait a moment (say 3 seconds here) before the callbackFunc() is called, and it is purely cosmetic, then comment the above and uncomment the below !
//setTimeout(callbackFunc, 3000);
}

myFunction = function(){        
          if (tot_codes > 0) {
              //setTimeout(function(){ 

                top.notification('successfully added the codes.');            
                //top.window.parent.document.getElementById('circularG').hide();
                window.parent.phFrntpayClosePopup();           
                //top.window.parent.document.getElementById("loaderHtml").style.display = "none";          
              //}, 3000); 

        } else {          
            top.notification('Please select atleast one code.');     
        }           
        };

function AddProcedures(thisval)
    {
        doAddProcedure(thisval, myFunction);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42837522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档