Stop jQuery function from executing









up vote
0
down vote

favorite












I want a save function to check if a session is set (using another function) and stop the base function from continuing, but the function seems to continue (not executable below):






//1. Base save function

$("#save").click(function (e)
e.preventDefault();
//step 0 check if logged in
checksession();
console.log("teststop"); //just for testing
//steps x continue execution of session exists
);

//2. Session check function
function checksession()
$.ajax(
type: "GET",
url:"xxxx/sessioncheck.php",
dataType: "text",
cache: false,
success: function(data)
if(data == "no")
return;
else //continue

);












share|improve this question



















  • 1




    $.ajax is asynchronous, that means the following code will continue executing and it's not blocking. Also even if this was intended to be a blocking call it wont work, return is only for the function it was called in. It doesn't propagate to the outer function that called and causes that one to return it nor should it.
    – Spencer Wieczorek
    Nov 10 at 8:02















up vote
0
down vote

favorite












I want a save function to check if a session is set (using another function) and stop the base function from continuing, but the function seems to continue (not executable below):






//1. Base save function

$("#save").click(function (e)
e.preventDefault();
//step 0 check if logged in
checksession();
console.log("teststop"); //just for testing
//steps x continue execution of session exists
);

//2. Session check function
function checksession()
$.ajax(
type: "GET",
url:"xxxx/sessioncheck.php",
dataType: "text",
cache: false,
success: function(data)
if(data == "no")
return;
else //continue

);












share|improve this question



















  • 1




    $.ajax is asynchronous, that means the following code will continue executing and it's not blocking. Also even if this was intended to be a blocking call it wont work, return is only for the function it was called in. It doesn't propagate to the outer function that called and causes that one to return it nor should it.
    – Spencer Wieczorek
    Nov 10 at 8:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want a save function to check if a session is set (using another function) and stop the base function from continuing, but the function seems to continue (not executable below):






//1. Base save function

$("#save").click(function (e)
e.preventDefault();
//step 0 check if logged in
checksession();
console.log("teststop"); //just for testing
//steps x continue execution of session exists
);

//2. Session check function
function checksession()
$.ajax(
type: "GET",
url:"xxxx/sessioncheck.php",
dataType: "text",
cache: false,
success: function(data)
if(data == "no")
return;
else //continue

);












share|improve this question















I want a save function to check if a session is set (using another function) and stop the base function from continuing, but the function seems to continue (not executable below):






//1. Base save function

$("#save").click(function (e)
e.preventDefault();
//step 0 check if logged in
checksession();
console.log("teststop"); //just for testing
//steps x continue execution of session exists
);

//2. Session check function
function checksession()
$.ajax(
type: "GET",
url:"xxxx/sessioncheck.php",
dataType: "text",
cache: false,
success: function(data)
if(data == "no")
return;
else //continue

);








//1. Base save function

$("#save").click(function (e)
e.preventDefault();
//step 0 check if logged in
checksession();
console.log("teststop"); //just for testing
//steps x continue execution of session exists
);

//2. Session check function
function checksession()
$.ajax(
type: "GET",
url:"xxxx/sessioncheck.php",
dataType: "text",
cache: false,
success: function(data)
if(data == "no")
return;
else //continue

);





//1. Base save function

$("#save").click(function (e)
e.preventDefault();
//step 0 check if logged in
checksession();
console.log("teststop"); //just for testing
//steps x continue execution of session exists
);

//2. Session check function
function checksession()
$.ajax(
type: "GET",
url:"xxxx/sessioncheck.php",
dataType: "text",
cache: false,
success: function(data)
if(data == "no")
return;
else //continue

);






jquery execution






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 8:00









Foo

1




1










asked Nov 10 at 7:47









Kevin Lindmark

2201515




2201515







  • 1




    $.ajax is asynchronous, that means the following code will continue executing and it's not blocking. Also even if this was intended to be a blocking call it wont work, return is only for the function it was called in. It doesn't propagate to the outer function that called and causes that one to return it nor should it.
    – Spencer Wieczorek
    Nov 10 at 8:02













  • 1




    $.ajax is asynchronous, that means the following code will continue executing and it's not blocking. Also even if this was intended to be a blocking call it wont work, return is only for the function it was called in. It doesn't propagate to the outer function that called and causes that one to return it nor should it.
    – Spencer Wieczorek
    Nov 10 at 8:02








1




1




$.ajax is asynchronous, that means the following code will continue executing and it's not blocking. Also even if this was intended to be a blocking call it wont work, return is only for the function it was called in. It doesn't propagate to the outer function that called and causes that one to return it nor should it.
– Spencer Wieczorek
Nov 10 at 8:02





$.ajax is asynchronous, that means the following code will continue executing and it's not blocking. Also even if this was intended to be a blocking call it wont work, return is only for the function it was called in. It doesn't propagate to the outer function that called and causes that one to return it nor should it.
– Spencer Wieczorek
Nov 10 at 8:02













2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










let checksession's AJAX success callback be the function that performs the "save" operation (or not)



//1. Base save function

$("#save").click(function (e)
e.preventDefault();
//step 0 check if logged in
checksession();
);


//2. Session check function

function checksession()
$.ajax(
type: "GET",
url:"xxxx/sessioncheck.php",
dataType: "text",
cache: false,
success: function(data)
if(data == "no")

console.log( "stop" );
else
console.log( "continue" );
doSave();


);



//3. Perform save operation

function doSave()
//
// proceed with the save operation
//






share|improve this answer



























    up vote
    0
    down vote













    Try this:



     $("#save").click(function (e) 


    e.preventDefault();
    //step 0 check if logged in
    check();
    if(check()==true)
    continue;
    else
    stop

    console.log("teststop"); //just for testing
    //steps x continue execution of session exists
    );

    //2. Session check function
    function check()
    var result=false;
    function checksession()
    $.ajax(
    type: "GET",
    url:"xxxx/sessioncheck.php",
    dataType: "text",
    cache: false,
    success: function(data)
    if(data == "no")
    return;
    else
    result=true;
    //continue

    );

    return result;







    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237020%2fstop-jquery-function-from-executing%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote



      accepted










      let checksession's AJAX success callback be the function that performs the "save" operation (or not)



      //1. Base save function

      $("#save").click(function (e)
      e.preventDefault();
      //step 0 check if logged in
      checksession();
      );


      //2. Session check function

      function checksession()
      $.ajax(
      type: "GET",
      url:"xxxx/sessioncheck.php",
      dataType: "text",
      cache: false,
      success: function(data)
      if(data == "no")

      console.log( "stop" );
      else
      console.log( "continue" );
      doSave();


      );



      //3. Perform save operation

      function doSave()
      //
      // proceed with the save operation
      //






      share|improve this answer
























        up vote
        1
        down vote



        accepted










        let checksession's AJAX success callback be the function that performs the "save" operation (or not)



        //1. Base save function

        $("#save").click(function (e)
        e.preventDefault();
        //step 0 check if logged in
        checksession();
        );


        //2. Session check function

        function checksession()
        $.ajax(
        type: "GET",
        url:"xxxx/sessioncheck.php",
        dataType: "text",
        cache: false,
        success: function(data)
        if(data == "no")

        console.log( "stop" );
        else
        console.log( "continue" );
        doSave();


        );



        //3. Perform save operation

        function doSave()
        //
        // proceed with the save operation
        //






        share|improve this answer






















          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          let checksession's AJAX success callback be the function that performs the "save" operation (or not)



          //1. Base save function

          $("#save").click(function (e)
          e.preventDefault();
          //step 0 check if logged in
          checksession();
          );


          //2. Session check function

          function checksession()
          $.ajax(
          type: "GET",
          url:"xxxx/sessioncheck.php",
          dataType: "text",
          cache: false,
          success: function(data)
          if(data == "no")

          console.log( "stop" );
          else
          console.log( "continue" );
          doSave();


          );



          //3. Perform save operation

          function doSave()
          //
          // proceed with the save operation
          //






          share|improve this answer












          let checksession's AJAX success callback be the function that performs the "save" operation (or not)



          //1. Base save function

          $("#save").click(function (e)
          e.preventDefault();
          //step 0 check if logged in
          checksession();
          );


          //2. Session check function

          function checksession()
          $.ajax(
          type: "GET",
          url:"xxxx/sessioncheck.php",
          dataType: "text",
          cache: false,
          success: function(data)
          if(data == "no")

          console.log( "stop" );
          else
          console.log( "continue" );
          doSave();


          );



          //3. Perform save operation

          function doSave()
          //
          // proceed with the save operation
          //







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 7:57









          Paolo

          9,443244766




          9,443244766






















              up vote
              0
              down vote













              Try this:



               $("#save").click(function (e) 


              e.preventDefault();
              //step 0 check if logged in
              check();
              if(check()==true)
              continue;
              else
              stop

              console.log("teststop"); //just for testing
              //steps x continue execution of session exists
              );

              //2. Session check function
              function check()
              var result=false;
              function checksession()
              $.ajax(
              type: "GET",
              url:"xxxx/sessioncheck.php",
              dataType: "text",
              cache: false,
              success: function(data)
              if(data == "no")
              return;
              else
              result=true;
              //continue

              );

              return result;







              share|improve this answer
























                up vote
                0
                down vote













                Try this:



                 $("#save").click(function (e) 


                e.preventDefault();
                //step 0 check if logged in
                check();
                if(check()==true)
                continue;
                else
                stop

                console.log("teststop"); //just for testing
                //steps x continue execution of session exists
                );

                //2. Session check function
                function check()
                var result=false;
                function checksession()
                $.ajax(
                type: "GET",
                url:"xxxx/sessioncheck.php",
                dataType: "text",
                cache: false,
                success: function(data)
                if(data == "no")
                return;
                else
                result=true;
                //continue

                );

                return result;







                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Try this:



                   $("#save").click(function (e) 


                  e.preventDefault();
                  //step 0 check if logged in
                  check();
                  if(check()==true)
                  continue;
                  else
                  stop

                  console.log("teststop"); //just for testing
                  //steps x continue execution of session exists
                  );

                  //2. Session check function
                  function check()
                  var result=false;
                  function checksession()
                  $.ajax(
                  type: "GET",
                  url:"xxxx/sessioncheck.php",
                  dataType: "text",
                  cache: false,
                  success: function(data)
                  if(data == "no")
                  return;
                  else
                  result=true;
                  //continue

                  );

                  return result;







                  share|improve this answer












                  Try this:



                   $("#save").click(function (e) 


                  e.preventDefault();
                  //step 0 check if logged in
                  check();
                  if(check()==true)
                  continue;
                  else
                  stop

                  console.log("teststop"); //just for testing
                  //steps x continue execution of session exists
                  );

                  //2. Session check function
                  function check()
                  var result=false;
                  function checksession()
                  $.ajax(
                  type: "GET",
                  url:"xxxx/sessioncheck.php",
                  dataType: "text",
                  cache: false,
                  success: function(data)
                  if(data == "no")
                  return;
                  else
                  result=true;
                  //continue

                  );

                  return result;








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 10 at 9:39









                  shaghayegh sheykholeslami

                  1847




                  1847



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237020%2fstop-jquery-function-from-executing%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo