我正在运行一个if语句,并检查一下与momentjs不同的日期。如果语句为真,我将如何停止整个函数的执行,然后在其中决定是继续整个函数还是退出?
例如,我有一个名为store的函数
$(document).on('click', '#new-booking-form input[type="submit"]', function(e){
e.preventDefault();
//If the difference in months is more than 3
if(moment($('input[name="my_input"]').val() , 'DD/MM/YYYY').diff(moment(), 'months') > 3){
swal({
title: 'Warning',
html: 'The starting date you have selected is more than 3 months in the future, is this correct?',
type: 'warning',
customClass: 'swal-logout',
showCancelButton: true,
confirmButtonText: 'Yes'
}).then((result) => {
if (result.value) {
return true;
}
});
}
//Do the submit here
})
我需要此功能的工作方式是
如何根据if
语句以及如果用户选择是或否来运行整个功能?
答案 0 :(得分:0)
不确定这是否是您要寻找的东西,但这听起来像是您在试图强行解决诺言
function errorHandler(error) {
throw(error)
};
if(moment($('input[name="my_input"]').val() , 'DD/MM/YYYY').diff(moment(), 'months') > 3){
swal({
title: 'Warning',
html: 'The starting date you have selected is more than 3 months in the future, is this correct?',
type: 'warning',
customClass: 'swal-logout',
showCancelButton: true,
confirmButtonText: 'Yes'
}).then((result) => {
if (result.value) {
return true;
}
}, errorHandler);
}
希望有帮助