The code execution continues as Sweetalert does not wait for the user's response

swal({title: "Are you sure?", text: "Your form will be submitted", confirmButtonText: "Ok",  
showConfirmButton:true, showCancelButton: true, cancelButtonText: "No", type:"warning", closeOnConfirm: false,
                    closeOnCancel: false },
                    function(isConfirm) {
                        if (isConfirm) {
                            swal({
                                title: 'Confirm!',
                                text: 'Successfully Submitted!',
                                type: 'success'
                            }, function() {
                                console.log("Inside Function"); 
                                $scope.applyleave("ok");
                            });

                        } else {
                            swal("Cancelled", "Your application not submitted :)", "error");
                            return false;
                        }
                    });     

The code flow should only continue after clicking 'Ok', but it currently executes without any user action.

I am submitting the form from HTML to this JS, but I want to block the confirmation box and wait for user action. I want to allow the below code to execute only when the user presses 'Ok'.

Answer №1

It's a common challenge in javascript event handling.

If you want to mimic a standard popup behavior, encapsulate the actions that should only occur after user input in a function. Then, call that function within the isConfirm function.

Standard popup example:

if (confirm("Are you sure?")) {
    console.log("Yes!");
}
else {
    console.log("No!");
}

console.log("Goodbye!");

Sweet Alert example:

sweetAlert({ title: "Are you sure?" },
    function(isConfirm) {
        if (confirm("Are you sure?")) {
            console.log("Yes!");
        }
        else {
            console.log("No!");
        }

        afterConfirm();
        // you can also directly write code here
    });

function afterConfirm() {
    console.log("Goodbye!");
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the globe with 3D raycasting, orbit controls, and dynamic camera rotation

It would be great if users could hover over the small spheres in different countries to get more information. Initially, I thought using a raycaster would help achieve this, but it's not responding to mouse movements as expected. It seems like the is ...

Using AngularJS to make a $http GET call with an array of parameters

Trying to make an $http.get request to my rest service using this code: $http({ method: 'GET', url: '/api/item/all', params: { query: { userid: 7 }, fields: 'title' } }); E ...

Getting just the outer edges of intricate BufferGeometry in Three.js

Currently, I am immersed in a project that involves zone creation and collision detection using Three.js. The primary objective is for my application to effectively manage collisions and produce a BufferGeometry as the final output. My aim is to visually r ...

I'm curious, in which environment does SvelteKit, Next.js, and Nuxt.js code execute? Also, is it possible to create HTTP request handlers within

My experience with using SvelteKit in my personal projects has been quite enjoyable. However, coming from a background of working with frameworks like Next.js and Nuxt.js, I often find myself confused about where the code I write actually runs. In my day ...

Use `Res.download()` to send data to the client instead of directly downloading the file

I am trying to transfer a file that has been created by my NodeJS server from the server to the client for download. Users input object data which is then stored in a database. The function responsible for creating the file will read these objects and pop ...

Multer can handle the uploading of various files from multiple inputs within a single form

I've searched everywhere on the internet, but I can't seem to find a solution that matches my specific issue. As someone new to Node.JS, I'm attempting to upload two different pictures using Multer from the same form. Here's what my for ...

Exploring Nested Data in MongoDB Aggregation using $match and $project

Struggling with crafting a Mongoose Aggregate Query to extract the permissions object of a specific member within a deeply nested structure of business data. MongoDB's documentation on this topic is lacking, making it hard for me to progress. Sample ...

What is the optimal offset to enhance the drag effect?

I have been working on making a draggable cloud, but I am facing an issue where the cloud always clips from the center to the mouse position. Here is the current setup: $(document).ready(function () { // code for cloud setup }); #background-canvas ...

Using Fabric JS to update the image source of an element with a new URL

Can the src attribute of a fabric js object be changed to a CDN url? { version: '4.4.0', objects: [ { type: 'image', version: '4.4.0', originX: 'left', ... src:'www.cdn ...

"Transferring cart controller information to the checkout controller: techniques and best practices

As a beginner using MySQL as a backend to store data, I am struggling with passing product_id, quantity, and price to the checkout page. Can someone provide guidance on how to store these data efficiently? Cart Service .factory('sharedCartService ...

Here's a guide on executing both GET and POST requests using a single form

Currently, I am developing a web application which involves using a GET request to display checkboxes in a form. The selected data from the checkboxes needs to be sent back to the server using a POST request. However, I'm facing an issue with performi ...

Incorporating AngularJS into JSP for Seamless Integration

As part of our application upgrade, we are transitioning from JSP to AngularJS one module at a time. One challenge we face is transferring user data from JSP to AngularJS (specifically index.html). We aim for a smooth transition where invoking the Angular ...

What is the best way to set conditions for document side script?

I'm struggling to disable the horizontal scroll when the viewport width is 480px or less. The script that controls the scroll behavior on my website looks like this: <script> $(function () { $("#wrapper").wrapInner("< ...

Is it possible to create a progress bar in AngularJS that features several different

Seeking recommendations: I am in need of displaying a bar with three statuses using different colors all within the same bar. Any ideas? ...

Having issues updating cookies with jQuery in ASP.NET framework

On my asp.net web page, I have implemented a search filter functionality using cookies. The filter consists of a checkbox list populated with various categories such as sports, music, and food. Using a jQuery onchange event, I capture the index and categor ...

Exclusive event triggered in Blazor only upon changing the page location

Just starting out with Blazor. I've been working on a Blazor project that utilizes DevExpress components, causing some issues with scrolling. My goal is to have the page start at the top every time I navigate to a new page. Currently, I've found ...

Send the value of a JavaScript variable to a PHP file upon calling a JavaScript function

When a script button is clicked, a javascript function runs. The function looks like this: //script function oyCrosswordFooter.prototype.update = function(){ var buf = ""; if (!this.puzz.started){ buf += "Game has not ...

Sending STATIC_URL to Javascript file in Django

What is the most effective method for transferring {{ STATIC_URL }} to JavaScript files? I am currently using django with python. Thank you in advance. Best regards. ...

Create a roster of individuals who responded to a particular message

Can a roster be created of individuals who responded to a specific message in Discord? Message ID : '315274607072378891' Channel : '846414975156092979' Reaction : ✅ The following code will run: bot.on("ready", async () = ...

Adjust Text to Perfectly Fit Button

I am developing a quiz using bootstrap and javascript. One issue I encountered is that the text in the buttons can sometimes be longer than the button itself. This results in the text not fitting properly within the button, making it unreadable on mobile ...