JQuery Ajax Anonymus Callback Function gets overwritten when called while ajax in progressHow can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callWhat's the easiest way to call a function every 5 seconds in jQuery?jQuery: Return data after ajax call successHow to make an AJAX call without jQuery?Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for itJquery Ajax callback not getting executed after geting response from server for JsonpCallback functions not getting calledIs there a callback for when an Angular DTOptionsBuilder ajax call finishes?How to call element.onload inside AJAX sucess function?

Did Michelle Obama have a staff of 23; and Melania have a staff of 4?

Why won't the Republicans use a superdelegate system like the DNC in their nomination process?

What is the proper name for a circle with a line through it?

Heyawake: An Introductory Puzzle

What is the opposite of "hunger level"?

Is this bar slide trick shown on Cheers real or a visual effect?

Why are electric shavers specifically permitted under FAR §91.21

What's a good pattern to calculate a variable only when it is used the first time?

Unconventional examples of mathematical modelling

Is there a word for returning to unpreparedness?

What should I do with the stock I own if I anticipate there will be a recession?

What was the intention with the Commodore 128?

Are there any cons in using rounded corners for bar graphs?

What allows us to use imaginary numbers?

Why does Japan use the same type of AC power outlet as the US?

If a person claims to know anything could it be disproven by saying 'prove that we are not in a simulation'?

Can someone with Extra Attack do a Commander Strike BEFORE he throws a net?

What modifiers are added to the attack and damage rolls of this unique longbow from Waterdeep: Dragon Heist?

Can anybody tell me who this Pokemon is?

What is a "soap"?

Go to last file in vim

What is the most difficult concept to grasp in Calculus 1?

List, map function based on a condition

Bringing Power Supplies on Plane?



JQuery Ajax Anonymus Callback Function gets overwritten when called while ajax in progress


How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callWhat's the easiest way to call a function every 5 seconds in jQuery?jQuery: Return data after ajax call successHow to make an AJAX call without jQuery?Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for itJquery Ajax callback not getting executed after geting response from server for JsonpCallback functions not getting calledIs there a callback for when an Angular DTOptionsBuilder ajax call finishes?How to call element.onload inside AJAX sucess function?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I have a timing or scope problem on this function call.. or no idea what.



 AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)


From this Function



function AjaxHandlerByClass(className, postData, callback, callbackFail) 

var timestamp = new Date().getTime();

var me = this;
me.backHandler = function (data) data.debug)
if (data.debug)
var debug = data.debug;
else if (data.responseJSON && data.responseJSON.debug)
var debug = data.responseJSON.debug;
if (window.console)
for (var key in debug)
if (debug.hasOwnProperty(key))
// console.log(debug[key]);





if (me.mode = 'callback')
callback(data); //<--- this is the bug location
else
callbackFail(data);

;

this.ok = function (data)
me.mode = 'callback';
me.backHandler(data)


this.notOk = function (data)
me.mode = 'callbackFail';
me.backHandler(data)


$.ajax(

contentType: "application/json; charset=utf-8",
url: className + '?ts=' + timestamp + '&sid=' + sid,
type: 'post',
data: JSON.stringify(postData),
dataType: 'json',
cache: false,
success: me.ok,
error: me.notOk

);



The first callback Function never gets executed, while the second one does get executed but 2 times.



The bug happens on the if (me.mode = 'callback') part of the code.
I already tried other options to make the callback function stuck right.



The first attempt was to store the callback function in the Function scope itself.
with assigning it to this.callback and then trying to access it via me.scope
which did not work. then I tried to access the variables directly.. and it is not helping either...



 this.callback = callback;
this.callbackFail = callbackFail;

var me = this;
me.backHandler = function (data) ;


I'm on my wit's end.










share|improve this question
































    -1















    I have a timing or scope problem on this function call.. or no idea what.



     AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
    AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)


    From this Function



    function AjaxHandlerByClass(className, postData, callback, callbackFail) 

    var timestamp = new Date().getTime();

    var me = this;
    me.backHandler = function (data) data.debug)
    if (data.debug)
    var debug = data.debug;
    else if (data.responseJSON && data.responseJSON.debug)
    var debug = data.responseJSON.debug;
    if (window.console)
    for (var key in debug)
    if (debug.hasOwnProperty(key))
    // console.log(debug[key]);





    if (me.mode = 'callback')
    callback(data); //<--- this is the bug location
    else
    callbackFail(data);

    ;

    this.ok = function (data)
    me.mode = 'callback';
    me.backHandler(data)


    this.notOk = function (data)
    me.mode = 'callbackFail';
    me.backHandler(data)


    $.ajax(

    contentType: "application/json; charset=utf-8",
    url: className + '?ts=' + timestamp + '&sid=' + sid,
    type: 'post',
    data: JSON.stringify(postData),
    dataType: 'json',
    cache: false,
    success: me.ok,
    error: me.notOk

    );



    The first callback Function never gets executed, while the second one does get executed but 2 times.



    The bug happens on the if (me.mode = 'callback') part of the code.
    I already tried other options to make the callback function stuck right.



    The first attempt was to store the callback function in the Function scope itself.
    with assigning it to this.callback and then trying to access it via me.scope
    which did not work. then I tried to access the variables directly.. and it is not helping either...



     this.callback = callback;
    this.callbackFail = callbackFail;

    var me = this;
    me.backHandler = function (data) ;


    I'm on my wit's end.










    share|improve this question




























      -1












      -1








      -1








      I have a timing or scope problem on this function call.. or no idea what.



       AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
      AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)


      From this Function



      function AjaxHandlerByClass(className, postData, callback, callbackFail) 

      var timestamp = new Date().getTime();

      var me = this;
      me.backHandler = function (data) data.debug)
      if (data.debug)
      var debug = data.debug;
      else if (data.responseJSON && data.responseJSON.debug)
      var debug = data.responseJSON.debug;
      if (window.console)
      for (var key in debug)
      if (debug.hasOwnProperty(key))
      // console.log(debug[key]);





      if (me.mode = 'callback')
      callback(data); //<--- this is the bug location
      else
      callbackFail(data);

      ;

      this.ok = function (data)
      me.mode = 'callback';
      me.backHandler(data)


      this.notOk = function (data)
      me.mode = 'callbackFail';
      me.backHandler(data)


      $.ajax(

      contentType: "application/json; charset=utf-8",
      url: className + '?ts=' + timestamp + '&sid=' + sid,
      type: 'post',
      data: JSON.stringify(postData),
      dataType: 'json',
      cache: false,
      success: me.ok,
      error: me.notOk

      );



      The first callback Function never gets executed, while the second one does get executed but 2 times.



      The bug happens on the if (me.mode = 'callback') part of the code.
      I already tried other options to make the callback function stuck right.



      The first attempt was to store the callback function in the Function scope itself.
      with assigning it to this.callback and then trying to access it via me.scope
      which did not work. then I tried to access the variables directly.. and it is not helping either...



       this.callback = callback;
      this.callbackFail = callbackFail;

      var me = this;
      me.backHandler = function (data) ;


      I'm on my wit's end.










      share|improve this question
















      I have a timing or scope problem on this function call.. or no idea what.



       AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)
      AjaxHandlerByClass('url', clientName: this.clientName, function (response) This code gets called 2 times)


      From this Function



      function AjaxHandlerByClass(className, postData, callback, callbackFail) 

      var timestamp = new Date().getTime();

      var me = this;
      me.backHandler = function (data) data.debug)
      if (data.debug)
      var debug = data.debug;
      else if (data.responseJSON && data.responseJSON.debug)
      var debug = data.responseJSON.debug;
      if (window.console)
      for (var key in debug)
      if (debug.hasOwnProperty(key))
      // console.log(debug[key]);





      if (me.mode = 'callback')
      callback(data); //<--- this is the bug location
      else
      callbackFail(data);

      ;

      this.ok = function (data)
      me.mode = 'callback';
      me.backHandler(data)


      this.notOk = function (data)
      me.mode = 'callbackFail';
      me.backHandler(data)


      $.ajax(

      contentType: "application/json; charset=utf-8",
      url: className + '?ts=' + timestamp + '&sid=' + sid,
      type: 'post',
      data: JSON.stringify(postData),
      dataType: 'json',
      cache: false,
      success: me.ok,
      error: me.notOk

      );



      The first callback Function never gets executed, while the second one does get executed but 2 times.



      The bug happens on the if (me.mode = 'callback') part of the code.
      I already tried other options to make the callback function stuck right.



      The first attempt was to store the callback function in the Function scope itself.
      with assigning it to this.callback and then trying to access it via me.scope
      which did not work. then I tried to access the variables directly.. and it is not helping either...



       this.callback = callback;
      this.callbackFail = callbackFail;

      var me = this;
      me.backHandler = function (data) ;


      I'm on my wit's end.







      javascript jquery






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 11:54







      Sangoku

















      asked Mar 27 at 11:49









      SangokuSangoku

      1,0022 gold badges14 silver badges42 bronze badges




      1,0022 gold badges14 silver badges42 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0














          First of, callBackFail is never defined in your code.



          function AjaxHandlerByClass(className, postData, callback, callbackFail)



          You pass in three parameters: className, postData and callback.



          AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)



          Second, this line should be me.mode === 'callback', not me.mode = 'callback'



          if (me.mode === 'callback') 
          callback(data);
          else
          callbackFail(data);



          You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass() to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.



          const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);

          const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);


          Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.



          I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.






          share|improve this answer

























          • This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call

            – Sangoku
            Mar 27 at 16:31











          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',
          autoActivateHeartbeat: false,
          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%2f55376487%2fjquery-ajax-anonymus-callback-function-gets-overwritten-when-called-while-ajax-i%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          First of, callBackFail is never defined in your code.



          function AjaxHandlerByClass(className, postData, callback, callbackFail)



          You pass in three parameters: className, postData and callback.



          AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)



          Second, this line should be me.mode === 'callback', not me.mode = 'callback'



          if (me.mode === 'callback') 
          callback(data);
          else
          callbackFail(data);



          You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass() to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.



          const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);

          const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);


          Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.



          I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.






          share|improve this answer

























          • This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call

            – Sangoku
            Mar 27 at 16:31
















          0














          First of, callBackFail is never defined in your code.



          function AjaxHandlerByClass(className, postData, callback, callbackFail)



          You pass in three parameters: className, postData and callback.



          AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)



          Second, this line should be me.mode === 'callback', not me.mode = 'callback'



          if (me.mode === 'callback') 
          callback(data);
          else
          callbackFail(data);



          You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass() to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.



          const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);

          const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);


          Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.



          I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.






          share|improve this answer

























          • This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call

            – Sangoku
            Mar 27 at 16:31














          0












          0








          0







          First of, callBackFail is never defined in your code.



          function AjaxHandlerByClass(className, postData, callback, callbackFail)



          You pass in three parameters: className, postData and callback.



          AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)



          Second, this line should be me.mode === 'callback', not me.mode = 'callback'



          if (me.mode === 'callback') 
          callback(data);
          else
          callbackFail(data);



          You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass() to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.



          const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);

          const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);


          Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.



          I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.






          share|improve this answer













          First of, callBackFail is never defined in your code.



          function AjaxHandlerByClass(className, postData, callback, callbackFail)



          You pass in three parameters: className, postData and callback.



          AjaxHandlerByClass('url', clientName: this.clientName, function (response) this code gets never called)



          Second, this line should be me.mode === 'callback', not me.mode = 'callback'



          if (me.mode === 'callback') 
          callback(data);
          else
          callbackFail(data);



          You've named your function AjaxHandlerByClass, I assume you want to use it as a class. You've declared it as function. Regular functions are executed when they are invoked(called) causing the second call to AjaxHandlerByClass() to render twice. To solve your problem you could either create a new instance of your AjaxHandlerByClass using the new keyword.



          const firstRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/people/", successCallback, failCallback);

          const secondRequst = new AjaxHandlerByClass('url', clientName: "https://swapi.co/api/planets/", successCallback, failCallback);


          Or if you want to wait for the first request to finish before calling the second request you could implement Promise. More on Promise here.



          I created a js-fiddle here with some modifications(swapped api and renamed some varaibles just for testing purpose.). One of the ajax-request is successful and the other fails. The result is visible in the developer-console. Note this fiddle is not perfectly written, its just some dummy code for demo purpose.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 16:16









          henrik123henrik123

          6085 silver badges12 bronze badges




          6085 silver badges12 bronze badges















          • This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call

            – Sangoku
            Mar 27 at 16:31


















          • This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call

            – Sangoku
            Mar 27 at 16:31

















          This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call

          – Sangoku
          Mar 27 at 16:31






          This is only a snippet. the code is functional. i did not post the whole code.. the gist is that the AjaxHandlerByClass function cant keep the reference to the object. And callbackFail and the other one are anonym function pased when you make the function call

          – Sangoku
          Mar 27 at 16:31









          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376487%2fjquery-ajax-anonymus-callback-function-gets-overwritten-when-called-while-ajax-i%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript