fabien-d / alertify.js

JavaScript Alert/Notification System
http://fabien-d.github.com/alertify.js/
4.26k stars 725 forks source link

Block program flow and wait for user input #145

Closed denis44 closed 11 years ago

denis44 commented 11 years ago

In my program I need to block the program flow until user make a choise.

function api_call_from_external_library(e) {

if(confirm("Do you really want to delete it?")) { return true; }

// the code here should be executed only after user made a choise, // not immediately

return false; });

If I use alertify just as described on the website, the dialog runs in background and the next statement will be executed. But I need block the workflow in this function until I have input from user. How can I do it?

PS I know it's better to run it in background, but in this case my function is called from external API and when I leave this function, I have to return a specific value. But I cannot return it until I have user input...

fabien-d commented 11 years ago

Unfortunately JavaScript is a non-blocking language and this is not possible. You'd have to adjust the code to use the callback.

alertify.confirm( 'question', function ( e ) {
    if ( e ) {
        // the code to execute after a user choice
    }
});
fabien-d commented 11 years ago

Closing - if you have more questions/comments, feel free to reopen.