It sounds like you are experiencing an issue where the SweetAlert2 modal automatically confirms after 2 seconds, even if you press the Cancel button. This behavior is not typical for SweetAlert2 and might be caused by some additional code or settings that are not shown in your example.
To help you better, let's ensure that your SweetAlert2 configuration is correct and doesn't include any unintended behavior. Here is a basic example of how to use SweetAlert2 with proper styling and functionality:
function myFunction() {
Swal.fire({
title: "Data lama akan dihapus!",
icon: "warning",
showCancelButton: true,
confirmButtonText: "Lanjut",
cancelButtonText: "Batal"
}).then((result) => {
if (result.isConfirmed) {
// Handle the confirm action
console.log("Confirmed");
} else if (result.isDismissed) {
// Handle the cancel action
console.log("Cancelled");
}
});
}
In this example:
-
showCancelButton: trueenables the Cancel button. -
confirmButtonText: "Lanjut"sets the text for the confirm button. -
cancelButtonText: "Batal"sets the text for the cancel button. - The
.then((result) => { ... })block handles the actions based on whether the user confirmed or canceled the alert.
If the alert is still automatically confirming after 2 seconds, check for any additional JavaScript code that might be interacting with the SweetAlert2 modal. For instance, ensure there are no setTimeout functions or other event listeners that might be triggering the confirm action.
If you have any additional code or settings that might be affecting the behavior, please share them so we can provide a more accurate solution.