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.
This betrays a misunderstanding of the sequence of events that occurs using asynchronous code. Just because you've written it inline doesn't mean it's going to execute strictly top-to-bottom.
You have written "//wait for response" as an alternative, but there is no JavaScript code you can write that will actually do that. Your function must return to give control back to the browser, before the browser can fire the click events on the jConfirm UI that make processing proceed.
Ways to make asynchronous code work in a synchronous context (and vice versa) exist - in particular threads and coroutines (and their limited relation generators). But JavaScript has none of these features, so you must write your code to fit the synchronous-or-asynchronous model your library is using.
This thing works. Needs jQuery.
CSS
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.
Think about callbacks as sending messages and it will result in better structure in your code.
Technically, yes, but I wouldn't do that on a website.
Take a look at Narrative JavaScript, which is based off Narcissus.
Selenium uses this technology.
Update
Check out JavaScript Strands:
Since the callback is asynchronous (at least, in the sense that it's waiting on the user to do something), it might be easier to handle what you need to inside the callback:
@klogan [comments]
I assume you got these from here?
The page gives you your answer: (look under Usage)
@klogan
The point I'm trying to make is that there isn't really an easy way to accomplish what you want. You're trying to correlate procedural and event-driven programming -- something JavaScript doesn't help you do.
The simplest (though, risky) solution is to use a pseudo-infinite-loop. But, ifcallback
never gets called, you now have an actual infinite loop. And, depending on the JavaScript engine, you might kill the browser waiting.Point: Your best bet is to avoid
thistrying to force event-driven into procedural.