最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript function callback after click event - Stack Overflow

programmeradmin1浏览0评论

This is my code

function confirm(){

    okBtn.on('click', function(){
        // return true from confirm function
    });
    cancelBtn.on('click', function(){
        // return false from confirm function
    });
}


if(confirm()){
    // make something. I need to keep this code here, not in confirm function
}

I need to execute callback after okBtn or cancelBtn are clicked. I need to keep callback code not in function, coz I have a lot of cases that use this function.

This is my code

function confirm(){

    okBtn.on('click', function(){
        // return true from confirm function
    });
    cancelBtn.on('click', function(){
        // return false from confirm function
    });
}


if(confirm()){
    // make something. I need to keep this code here, not in confirm function
}

I need to execute callback after okBtn or cancelBtn are clicked. I need to keep callback code not in function, coz I have a lot of cases that use this function.

Share Improve this question asked Jan 14, 2015 at 9:42 Sergey KudryashovSergey Kudryashov 7233 gold badges9 silver badges31 bronze badges 5
  • 1 What behaviour are you trying to achieve? The pattern used here will never work in JS for multiple reasons. – Rory McCrossan Commented Jan 14, 2015 at 9:43
  • There is already one function "confirm("message")" which do the same. – Bharadwaj Commented Jan 14, 2015 at 9:45
  • What is your question? – Jan Doggen Commented Jan 14, 2015 at 9:46
  • I have some cases, where I need to show a confirm popup. If it's confirmed, I need to continue my code, if no - stop it. – Sergey Kudryashov Commented Jan 14, 2015 at 9:46
  • search about "JavaScript confirm" – Bharadwaj Commented Jan 14, 2015 at 9:48
Add a ment  | 

4 Answers 4

Reset to default 5

Make the confirm function accept the callback and invoke it inside the event handlers:

function confirm(callback){
    okBtn.on('click', function(){
        callback(true);
    });
    cancelBtn.on('click', function(){
        callback(false);
    });
}


confirm(function(result) {
    // make something. I need to keep this code here, not in confirm function
});

But there is no way to deal with event handlers synchronously (like you seem intend to do). The very reason why you have to bind event handlers is that you don't know when the events occur. Instead you have to react to the event. You have to get used to this way of coding, there is no way around.

Instead of using an extra function, you could simply create a flag and change it based on user action:

var confirm_flag; // this is undefined, which means that no actions have been taken

okBtn.on('click', function(){
    validate(true);
});
cancelBtn.on('click', function(){
    validate(false);
});

Then, based on the user input, you can determine which course of action you want to take:

function validate(flag) {
    if(flag) {
        alert("okBtn has been clicked, it's safe to continue");
    }else{
        alert("Not good!");
    }
}

If you want to use the return value from a confirm popup, you can simply assign the Window confirm() function to a variable - if the user clicks ok, it will return true

var x = confirm('Is it safe?'); // will pop a confirm dialog box and return true if ok is pressed and false if cancel is pressed

http://jsfiddle/abwzq32s/

html 

<button type="button" id='button1'>confirm</button>
<button type="button" id='button2'>cancel</button>



js 

$('#button1').click(function(){
confirm(true);
});
$('#button2').click(function(){
    confirm(false);
});

function confirm(check){
    if(check){
        alert('ok');
    }else{
        alert('cancel');
    }
}

I did a small sample for you. You can either write your code inside the if condition or call another function if your function is really long

As per my understanding what you want is to execute some code only when the user click on the okBtn and some code when the user click on cancelBtn. If that you can change your code in this way

function confirm(callback)
{

    okBtn.on('click', function(){
        // return true to the callback
        callback(true)
    });
    cancelBtn.on('click', function(){

        // return false to the callback
         callback(false)
    });
}
confirm(function(confirmed){
    if(confirmed)
    {
    // make something
    }
    else
    {
    // do something
    }
});
发布评论

评论列表(0)

  1. 暂无评论