有人能告诉我这个自定义javascript函数是如何执行的吗?

有人能告诉我这个自定义javascript函数是如何执行的吗?,javascript,jquery,Javascript,Jquery,当我单击“取消按钮”时,我没有得到return$.Deferred对象。 我对此感到困惑。单击“取消按钮”后,我会得到$.Deferred对象和$.Deferred对象 //javascript代码 var str_confirmDialog = [ '<div class="tanChuang3" style="display: block;">', '<div class="oh pa table">', '<div class="table-cell"

当我单击“取消按钮”时,我没有得到return$.Deferred对象。 我对此感到困惑。单击“取消按钮”后,我会得到$.Deferred对象和$.Deferred对象

//javascript代码

var str_confirmDialog = [
'<div class="tanChuang3" style="display: block;">',
'<div class="oh pa table">',
    '<div class="table-cell">',
        '<div class="bg-fff pr">',
            '<p class="tl"><strong>{title}</strong></p>',
            '<p class="tableCellTxt">{msg}</p>',
            '<p class="tr">',
                '<a href="javascript:;" class="_cancel">cancel</a>',
                '<a href="javascript:;" id="close" class="_next">OK</a>',
            '</p>',
        '</div>',
    '</div>',
    '<i class="close pa c-close-btn"></i>',
'</div>',
'<div class="tanChuangBg"></div>',
'</div>'].join('');
Hnb.ui = {

showConfirm: function(title , msg , sleep) {
            var defer = $.Deferred();
            var str_message = str_confirmDialog.replace('{title}' , title);
            var str_message = str_message.replace('{msg}' , msg);
            var $box = $(str_message).appendTo("body");

            $box.find(".c-close-btn").click(function() {
                $(this).unbind('click');
                $box.remove();
                defer.resolve();
            });

            // cancel button
            $box.find("._cancel").click(function(){
                $(this).unbind("click");
                $box.remove();
                // //If I do not comment this paragraph, it will be fine to execute always.
                //return defer.reject();
            }); 
            // next button
            $box.find("._next").click(function() {
                $(this).unbind("click");
                $box.remove();
                defer.resolve();
            });

            if(sleep) {
                setTimeout(function() {
                    $box.find('.c-close-btn').trigger('click');
                } , sleep);
            }
            return defer;
        }
}

如果要在单击“取消”时拒绝承诺,则不返回,只需调用
reject
(就像所有其他单击处理程序一样,只需调用
reject
resolve
):

下面是一个活生生的例子,我完成了上面的
defer.reject()
,并注释掉了自动关闭;很好用:

var Hnb={};//补充
变量str_confirmDialog=[
'',
'',
'',
'',
“

{title}

”, “

{msg}

”, “

”, '', '', “

”, '', '', '', '', '', '' ].加入(“”); Hnb.ui={ showConfirm:功能(标题、消息、睡眠){ var defer=$.Deferred(); var str_message=str_confirmDialog.replace('{title}',title); var str_message=str_message.replace({msg}',msg); var$box=$(str_message).appendTo(“body”); $box.find(“.c-close-btn”)。单击(函数(){ $(this.unbind('click'); $box.remove(); defer.resolve(); }); //取消按钮 $box.find(“.\u cancel”)。单击(函数(){ $(此)。解除绑定(“单击”); $box.remove(); defer.reject(); }); //下一个按钮 $box.find(“.\u next”)。单击(函数(){ $(此)。解除绑定(“单击”); $box.remove(); defer.resolve(); }); 如果(睡眠){ setTimeout(函数(){ $box.find('.c-close-btn').trigger('click'); }睡眠); } 返回延迟; } }; Hnb.ui.showConfirm('title','msg'/*,3000*/).done(function(){ $(“”).text('done').appendTo(document.body); }).always(函数(){ $(“”).text('always').appendTo(document.body); });
它不工作。$box.remove()代码似乎无法执行。@user39291:是的,它可以工作。我添加了一个实例。我有一个问题,如果我不延迟。拒绝(),但我不能执行到始终()。根据jQuery文档,始终将执行任何拒绝(),解析,挂起。@user39291:“根据jQuery文档,始终将执行任何拒绝(),解析,挂起”不挂起,不。只需解析或拒绝即可。它是:
done
-仅在解析时调用<代码>失败
-仅在被拒绝时调用<代码>始终-始终调用。还有
then
,它接受三个回调(已解决、已拒绝、进度)。
Hnb.ui.showConfirm('title' , 'msg' , 3000).done(function(){
   alert('done');
}).always(function() {
   alert('always');
});
// cancel button
$box.find("._cancel").click(function(){
    $(this).unbind("click");
    $box.remove();
    defer.reject(); // <== No return
}); 
Hnb.ui.showConfirm('title', 'msg', 3000)
// Here ---------------------------^