I'm trying to use the jQuery alerts dialog library from http://abeautifulsite.net/notebook/87 instead of the default alerts (which look pretty awful in my opinion). This seems to be a great library, but there is not an example of how to use the jConfirm library.
I need to do something like this:
function confirm() { var result = false; var response = false; jConfirm('are you sure?', 'Confirmation Dialog', function(r) { result = r; response = true; return r; }); if (response == true) { alert(result); return result; } else { //wait for response alert('hi'); } }
and my call from my .net button:
I've posted a comment on the plugin's website (just this morning) and did Google searches for javascript and waiting for a callback to complete with no results.
Any ideas on how to use the callback correctly to get the result, before the rest of the javascript executes?
Thanks.
Use async/await to Wait for a Function to Finish Before Continuing Execution. Another way to wait for a function to execute before continuing the execution in the asynchronous environment in JavaScript is to use async/wait .
But in some case, Javascript behaves as asynchronous, such as in AJAX or Axios calls. It makes a call to the API but does not wait for the API to return result, and progresses with the next queued event.
The setTimeout() function implements a delay before executing a specific Javascript function. It requires two parameters: a post-delay function to be run and the delay time. The post-delay function must be defined in the Javascript file before being called by setTimeout() .
Sooner or later, you will come across the need to make JavaScript wait until a function has finished before continuing to execute more code. In JavaScript this can sometimes be challenging, because JavaScript doesn't wait for code that it knows will take some time to complete.
You've just hit a big limitation in JavaScript. Once your code enters the asynchronous world, there is no way to get back to a classic procedural execution flow.
In your example, the solution would be to make a loop waiting for the response to be filled. The problem is that JavaScript does not provide any instruction that will allow you to loop indefinitely without taking 100% of the processing power. So you will end up blocking the browser, sometimes to the point where your user won't be able to answer the actual question.
The only solution here is to stick to the asynchronous model and keep it. My advice is that you should add a callback to any function that must do some asynchronous work, so that the caller can execute something at the end of your function.
function confirm(fnCallback) { jConfirm('are you sure?', 'Confirmation Dialog', function(r) { // Do something with r fnCallback && fnCallback(r); // call the callback if provided }); } // in the caller alert('begin'); confirm(function(r) { alert(r); alert('end'); })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With