我正在使用jquery"when,then"来做一些操作。我正在使用doAddProcedure函数来执行一些计算。我期待一个结果,例如在 doAddProcedure 函数中执行代码后,控件将返回到 AddProcedures,然后返回到"完成"部分中的代码。但它没有按预期工作。此外,我还展示了一个加载器,以便在执行doAddProcedure部分中的代码期间显示。加载程序未显示执行 doAddProcess 中的代码所花费的时间。请帮助我解决问题。对不起我的英语
这是我的代码
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
这是因为当您不向when
传递延迟或承诺时,then
会立即执行。查看 jquery 文档以了解 When..然后
在您的情况下,您将when()
视为某种等待true
if
条件......我建议你阅读更多关于Promise
,Deferred
然后回到when..then
逻辑或使用承诺。
你当然可以使用如下回调函数:
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);
}