0

I've read the following post about how to exit from a javascript function:

How can I exit from a javascript function?

I'm trying to adapt the example to my code, but I'm missing something / doing something wrong.

I have the following jquery code:

    $('#main_submit_button').live('click', function(){  

        check_for_duplicate_rule().done(function(result) {
                    console.log("result from check for duplicate" + result);
                    if (result == 'true') {                 
                        $('#validation_error').html("A similar rule already exists");
                        return;
                    }
            });
        //}
        console.log("i made it this far");
        return false;
        console.log($('#rule_form').serialize() );

}

When I test using duplicate data, the system correctly displays an error that says "A similar rule already exists" but it also displays the message "i made it this far" in the console. It DOES NOT display the results of the form serialize() command. I was expecting the code to just exit the .click even handler after setting the error message.

I've tried change to change the "return" statement to "return false" but that doesn't work either. Can you tell me what I'm doing wrong? Ultimately, what I'd like this routine to do is perform several client side validation checks before i submit the data to the server. So I will have multiple points in the function where I need to "return" or exit.

Thanks

EDIT 1

I've changed the code to look like:

            var bduplicate = false;
            check_for_duplicate_rule().done(function(result) {
                    console.log("result from check for duplicate" + result);
                    if (result == 'true') {                 
                        $('#validation_error').html("A similar rule already exists");
                        bduplicate = true
                        return;
                    }
            });
        //}
        if (bduplicate) {
            return;
        }
        console.log("i made it this far");

But it still prints the message "i made it this far". I think the problem might be with my the check_for_duplicate_rule function. ?? It looks like this:

    function check_for_duplicate_rule() {
            var parameters = {
                  num: $('#num').val(),
                  condition: $('#condition').val(),
                  cdidnumber: $('#cdidnumber').val()
            }   

            return $.getJSON(       
                    url = aURLdefinedsomewhere,
                    parameters,
                    function(data)  {
                            //if (data=='true') {                               
                                //$('#validation_error').html("A similar rule already exists");     
                            //}
                    }//end data
                );//end getJSON     
        }

What I'm seeing in the console is that the "result from check for duplicate true" message is being printed AFTER the "i made it this far", which seems to indicate that I'm checking for results that I don't have... ? If this is true, i don't really know how to go about fixing it.

Community
  • 1
  • 1
mark li
  • 347
  • 1
  • 7
  • 22

3 Answers3

0

Replace return false with return and then the console.log after that will be undefined!!!

The return exits the function returning undefined.

 $('#main_submit_button').live('click', function() {
        check_for_duplicate_rule().done(function(result) {
            console.log("result from check for duplicate" + result);
            if (result == 'true') {                 
                $('#validation_error').html("A similar rule already exists");
                return;
            }
        });
    //}
        console.log("i made it this far");
        return;//replace return false with return.
        console.log($('#rule_form').serialize() );//this will be undefined
    }

NOTE:return returns undefined, not false

  • break:for exiting loop
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
0

To expand on @tymeJV's comment, your first return is returning the done function asynchronously. It has no effect on the rest of the method.

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
  • thanks. can you give me an example of how to change my code? I'm going to actually move this validation check out of the submit click event.. but i still am not clear on how to make the async call and then test for the result.. whenever it comes back. – mark li Jul 29 '13 at 14:50
0

return inside of .done(function(result) { }) just exits from this function, not from .live('click', function(){ })

you should rewrite the code like this

$('#main_submit_button').live('click', function(){  

    result = check_for_duplicate_rule()
    console.log("result from check for duplicate" + result);
    if (result == 'true') {                 
                    $('#validation_error').html("A similar rule already exists");
                    return;
    }
    console.log("i made it this far");
    return false;
    console.log($('#rule_form').serialize() );
Liam
  • 27,717
  • 28
  • 128
  • 190
  • serg123e, I added some more details to my post. The check_for_duplicate_rule is making an ajax / getJSON call... which I why I had the .done() method. Does your answer still apply? – mark li Jul 29 '13 at 14:45
  • As you rewrite the code it should work too. but result of (true == 'true') is always false, that is why you get " "i made it this far" in console now – serg123e Jul 29 '13 at 15:00