【发布时间】:2020-08-13 09:58:55
【问题描述】:
我构建了一个使用 Toastr 创建警告通知的函数。 (用 JS 编写,不使用 Angular)。
它询问您是否要删除文件。
如果用户按下“YES”按钮,我想返回布尔值:“True”
如果用户按下“NO”按钮,我想返回布尔值:“False”
问题:
当我调用函数 fileAlert 时,我没有得到布尔值。
调用函数:
async function noticeUser() {
let promise = new Promise(() => {
fileAlert();
});
let result = await promise;
console.log("The result was: "+result); //after execution: result is empty
};
函数fileAlert():
<script>
function fileAlert() {
var userAnswer = toastr.warning('Are you sure you want to delete the file?<br /><br /><button type="button" id="yesDelete" class="btn clear">Yes</button> <button type="button" id="noDelete" class="btn clear">No</button>', "Warning");
var userSaid = true;
toastr.options = {
"closeButton": false,
"allowHtml": true,
onShown: function (toast) {
$('#yesDelete').click(function () {
console.log('user clicked yes - he wants to delete the file')
toastr.clear(userAnswer);
return userSaid;
});
$('#noDelete').click(function () {
console.log('user clicked no - he doesnt want to delete the file')
//Deleting warning message from screen.
toastr.clear(userAnswer);
//Returning False so that deletion procedure will not occur
userSaid= false;
return userSaid;
});
},
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": 0,
"extendedTimeOut": 0,
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut",
"tapToDismiss": false
}
};
</script>
【问题讨论】:
标签: javascript asp.net-mvc promise async-await toastr