1

I am trying to populate different cells in a table, named as #RECALRow1, #RECALCol1, #RECALBodySum. Each is populated from a database. I am using AJAX and, specifically, jQuery's load.

Originally I used a number of functions - see version 1 below - which worked (the code in these functions in effectively in version 2). This worked.

I belatedly realised how similar the code in these functions was. Version 2 shows the code without functions, illustrating the similarity. This worked too. (valTable is defined earlier - the definition is not shown below).

It then seemed "obvious" that I should write a generic function which just takes two parameters. Making three calls to this function, with different parameters, should surely work(!) In fact only the third function seems to have been called; the first two do not even succeed in generating a console message.

I wondered if I am missing something on callbacks - and I read How do I return the response from an asynchronous call? - but I cannot see that I need them. Perhaps I am about to learn something very basic on practical AJAX.

Version 1

Individual functions, each of which uses jQuery load. THIS WORKS. [Aside - ASP sets the default selected value]

UpdateCol1Possibilities(); // sets content for #RECALCol1
UpdateRow1Possibilities();
UpdateBodySumPossibilities();

Version 2

Direct call to jQuery load, without encapsulation. THIS WORKS. [Aside - we need to tweak the default selected value]

$('#RECALRow1').load(
    "/_RECAL/AJAX/AJAXSetGroupbyList.asp", // URL
    { // data - no need for callback
    "RECALtable":valTable,
    "RECALCol1Row1Id":'RECALRow1'
}); // close load

$('#RECALCol1').load(
    "/_RECAL/AJAX/AJAXSetGroupbyList.asp", // URL
    { // data - no need for callback
    "RECALtable":valTable,
    "RECALCol1Row1Id":'RECALCol1'
}); // close load                   

$('#RECALBodySum').load(
    "/_RECAL/AJAX/AJAXSetGroupbyList.asp", // URL
    { // data - no need for callback
    "RECALtable":valTable,
    "RECALCol1Row1Id":'RECALBodySum'
}); // close load                   

Version 3

Generic function, which uses jQuery load. THIS DOESN'T WORK.

var RealSelect = _.debounce(function(IdToChange) { 
console.log('Calling RealSelect changing Id: ' + IdToChange);
$('#' + IdToChange).load(
    "/_RECAL/AJAX/AJAXSetGroupbyList.asp", // URL
    { // data
        "RECALtable":$('#RECALtable').children().val(),
        "RECALCol1Row1Id":IdToChange
    }
    , // callback
    function() { // callback function - success
        //  alert('successful callback!');
    } // close callback function, close load
    ) // close load
}
,50); // end RealSelect function

RealSelect('RECALCol1'); // sets content for #RECALCol1
RealSelect('RECALRow1');                
RealSelect('RECALBodySum');
//  Only #RECALBodySum is populated
}               
Community
  • 1
  • 1
coolactuary
  • 53
  • 1
  • 9
  • 1
    You should read [**This**](https://github.com/rwaldron/idiomatic.js) – adeneo Jan 06 '15 at 19:16
  • @adeneo - very cool! I'm glad I came across your comment. I've got to pass this along to some of my coworkers! – Howard Renollet Jan 06 '15 at 19:28
  • @adeneo - thanks. There's some useful material there. I can't help but noticing that much of it was about code formatting. Was that your point? (Apologies, this was my first SO question and I found the code tool tricky to use, resulting in odd formatting - worse than the original and not sorted through the preview). Anything that article contributes to the result rather than the display of my code? – coolactuary Jan 06 '15 at 22:29
  • The code just looked really strange, and was hard to read, not sure if it's the mix of upper and lowercase, both in URL's, selectors and variables, the comments or what it is, but I thought maybe reading a little bit about styles and best practices would be useful. It wasn't really criticism, but sticking with lowercase, camelcase, and a style that most people are used to, makes it easier to read, and in turn easier to get help. As for the question, I have a suspicion the debounce is the issue, cancelling the other calls as it's trying to limit (debounce) the amount of calls to the function. – adeneo Jan 06 '15 at 23:22
  • @adeneo Thanks for the clarification. Styling is something I could improve (along with basic syntax!!) On my specific question, the debounce was indeed what was kicking in and stopping things working. As you probably guessed it was "left over" from other code, where it did play a key role. Even there I was using it as a trick without real understanding :( Thanks *so* much. Just got to find how to mark complete now! – coolactuary Jan 07 '15 at 00:05
  • As noone has a posted an answer, there's nothing to accept, yet! Post the solution you ended up with yourself, and accept your own answer, that leaves the question and answer for anyone else looking for the same thing, and you get some points. – adeneo Jan 07 '15 at 00:14
  • I attempted an answer. As Flexo (with 42K+ reputation) has deleted what I perceive to be an answer - certainly one that solved my problem - perhaps I will at least be allowed to post in the comment. adeneo thanks for your constructive approach. Effectively answered by adeneo: "I have a suspicion the debounce is the issue, cancelling the other calls as it's trying to limit (debounce) the amount of calls to the function" Absolutely right. Removing the debounce wrapping function solves my issue; debounce was blocking subsequent function calls. – coolactuary Jan 10 '15 at 12:19
  • Oops, didn't see the comments. Well, I hope my answer helps somebody else. Would be glad if you could accept it. :) – Guilherme Rodrigues Jan 14 '15 at 11:39

1 Answers1

0

I'm not sure why you felt you should use _.debounce, but that is precisely your problem, as far as I can see.

Remove the debounce wrapper in your function declaration and all should work:

var RealSelect = function(IdToChange) { 
console.log('Calling RealSelect changing Id: ' + IdToChange);
$('#' + IdToChange).load(
    "/_RECAL/AJAX/AJAXSetGroupbyList.asp", // URL
    { // data
        "RECALtable":$('#RECALtable').children().val(),
        "RECALCol1Row1Id":IdToChange
    }
    , // callback
    function() { // callback function - success
        //  alert('successful callback!');
    } // close callback function, close load
    ) // close load
}; // end RealSelect function

Debounce means precisely that your function will NOT be called until a certain amount of time has passed where no new calls were made.

This is the expected behaviour, for example, of an autocomplete widget. You don't want to process every keystroke the user does immediately. Instead, you wait for the user to stop typing for, say, 500 millis, and then start fetching the data from the server.

In your case, you are creating a debounced function and then calling it three times in a row. Debounce is working as expected - only the last call runs, and only after 50 millis.

From the underscorejs docs:

debounce_.debounce(function, wait, [immediate])

Creates and returns a new debounced version of the passed function which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked. Useful for implementing behavior that should only happen after the input has stopped arriving. For example: rendering a preview of a Markdown comment, recalculating a layout after the window has stopped being resized, and so on.

Pass true for the immediate parameter to cause debounce to trigger the function on the leading instead of the trailing edge of the wait interval. Useful in circumstances like preventing accidental double-clicks on a "submit" button from firing a second time.

var lazyLayout = _.debounce(calculateLayout, 300); $(window).resize(lazyLayout);

Community
  • 1
  • 1
Guilherme Rodrigues
  • 2,818
  • 1
  • 17
  • 22
  • You're absolutely right (and @adeneo also put me on the right track). The reason I was using debounce was historic; I was concerned that in IE and Chrome holding the arrow up / down key on a SELECT will trigger events and result in crashes. It did at the time and debounce seemed to solve that problem. But after a rewrite I don't need debounce to solve that it seems. – coolactuary Jan 14 '15 at 11:40