简体   繁体   中英

how to return true or false when i code window.confirm myself

Code:

 (function (proxied) {
    window.confirm = function (msg) {
        noty({
            text: '<i class="fa fa-exclamation-triangle">'+msg+'</i>',
            theme: 'relax',
            dismissQueue: true,
            layout: 'center',
            buttons: [
                {
                    addClass: 'btn btn-success btn-circle', text: '<i class="fa fa-check"></i>', onClick: function ($noty) {
                        $noty.close();
                        return true;
                    }
                },
                {
                    addClass: 'btn btn-danger btn-circle', text: '<i class="fa fa-times"></i>', onClick: function ($noty) {
                        $noty.close();
                        return false;
                    }
                }
            ]
        });
        //return proxied.apply(this, arguments);
    };
})(window.confirm);

It can not correct return true or false, I guess it may be the buttons closures ?Thanks everyone.

Only the native dialog functions can pause JavaScript execution until some action is taken which means you won't be able to return anything. You will have to use callbacks:

(function (proxied) {
    window.confirm = function (msg, callback) {
        noty({
            text: '<i class="fa fa-exclamation-triangle">'+msg+'</i>',
            theme: 'relax',
            dismissQueue: true,
            layout: 'center',
            buttons: [
                {
                    addClass: 'btn btn-success btn-circle', text: '<i class="fa fa-check"></i>', onClick: function ($noty) {
                        $noty.close();
                        callback(true);
                    }
                },
                {
                    addClass: 'btn btn-danger btn-circle', text: '<i class="fa fa-times"></i>', onClick: function ($noty) {
                        $noty.close();
                        callback(false);
                    }
                }
            ]
        });
        //return proxied.apply(this, arguments);
    };
})(window.confirm);

To use your function you will then have to do this:

window.confirm("Do you want ice cream?", function(result){
    if(result){
        // The user wants ice cream
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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