0

i want to stop executing javascript code after the line "return false",but it don't.

my english is poor. if anyone understand me,please improve my question thank you!

    $('#category-list input').blur(function(){
        var $$ = $(this), $p = $$.prev();
        if ($$.val() != $p.html()) {
            var data = {'key':$$.attr('name'),'value':$$.val()};
            $.post('<?php echo Url::to(['field']); ?>', data).done(function(result){
                if (result != 'ok'){
                    alert(result);
                    return false; /*i need it stop at here!!!!!! but it not*/
                }
            });
            alert(3);
            $p.html($$.val());
        }
        $$.hide();
        $p.show();
    });
Xiu Hong
  • 181
  • 2
  • 8
  • Do you want alert(3) to get executed after you get a response from the server? do you want $$.hide() to get executed after you get a response from the server? etc. Please could you let us know what bits of the code should or shouldn't be executed – Don Oct 22 '15 at 03:15
  • 1
    `$.post()` acts asynchronously, in its own time, without blocking the surrounding code. The code simply doesn't execute entirely in the order it was written in. The `alert(3);` will always execute before the `.done(function (result) {...})` callback is invoked. A good explanation of this behavior can be found in "[How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call)" – Jonathan Lonowski Oct 22 '15 at 03:15
  • Declaring a function inside another function doesn't have any impact on the outer function at all. Consider: `function outer() { function inner() { return 42; }; return 21; }`. What do you think `outer()` will return? – Felix Kling Oct 22 '15 at 03:33

1 Answers1

2

The short answer is you can't stop a parent function inside an ajax completion function because that will most likely fire after the function itself is finished executing. Regardless, its 2 separate events

$.post is an asynchronous ajax request. This means, that the .done() part of the function will execute AFTER the ajax request comes back.

However, the rest of the function runs synchronously, meaning every (or some) line of code after .post will run before the server even responds. To get around this, you need to re-architect how you execute your functions.

let's examine this with a basic function:

function getSomeAjax() {
  //1 - the alert will execute first
  alert('first line of execution');
  //2 - the post request will execute second
  $.post('someUrl', data).done(function(result){
     if (result != 'ok'){
       alert('post request complete');
       //this will execute randomly whenever the server responds. 
       //it could be in 1 second, 5 seconds, or 20 seconds
     }
  });
  //3
  alert('last line of execution');
}

looking at the example above, you should move any logic that is dependent on the server response into the if clause. Otherwise it will execute regardless of your post request

Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127