我使用jquery的"when,Then“来做一些操作。我正在使用doAddProcedure函数执行一些计算。我期待一个结果,比如在执行doAddProcedure函数中的代码后,控制将返回到AddProcedures,然后返回到完成部分.But中的代码,它没有按预期工作。此外,我还展示了一个加载器,以显示在doAddProcedure部分的代码执行时间。在doAddProcedure中执行代码所用的时间内,加载器未显示。请帮我修一下我的英语issue.Sorry
这是我的代码
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 发布于 2017-03-16 23:07:55
这是因为,当您没有向when传递延迟或承诺时,then将立即执行。查看When..Then的jquery文档
在您的示例中,您将when()视为等待true的某种if条件……我建议你阅读更多关于Promise和Deferred的知识,然后回到when..then逻辑或使用Promise。
当然,你也可以像下面这样使用回调函数:
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);
}https://stackoverflow.com/questions/42837522
复制相似问题