2

I have followed all the advice I found here at Wait until all jQuery Ajax requests are done? but I must be missing something basic.

I have this function...

function ListScores(leaguex, nameid) {
    return $.ajax({
        url: 'sg_ajaxListData.php',
        data: {
            nme: nameid,
            league: leaguex
        },
        dataType: 'json',
        success: function (rows) {
            for (var i in rows) {
                var row = rows[i];
                var score1 = row["score1"];
                var score2 = row["score2"];
                var round = row["round"];
                $('#output').append("<br />Round " + round + " - " + score1);
                if (leaguex == 'r') {
                    $('#output').append(" " + score2);
                }
            }
        }
    });
}

It is called from the document.ready function of my code and works perfectly, returning the data and displaying it as expected. I want another function to run AFTER ListScores() has displayed its data so, elsewhere in the document.ready function I put...

$.when(ListScores()).done(function(a1){
console.log("hello",leaguex,nameid, a1);
});

... but nothing ever shows up in the console log so it looks like this function is never being called. Am I missing something obvious here?

Thanks Steve

Community
  • 1
  • 1
sclg
  • 56
  • 5
  • By passing `ListScores()` you're trying to call the function – hjpotter92 Apr 02 '14 at 13:59
  • 1
    You don't really need the `.when` there, you could just do `ListScores().done(function() {...})`, but it should work as is.. check your console, and you might try adding `.fail()` and `.always()` callbacks to see if either of those trigger. – Jason P Apr 02 '14 at 14:00
  • @hjpotter92 But `ListScores()` returns a promise, so it should work. – Jason P Apr 02 '14 at 14:01
  • 1
    Can you show all of the code? One thing I noticed is that you don't pass any parameters to your LightScores function in $.when. Do you see request go through successfully? – milagvoniduak Apr 02 '14 at 14:02
  • Agreed with Jason P, you don't really need $.when in this case. – milagvoniduak Apr 02 '14 at 14:03
  • @MyP3uK Completely missed that, I bet that's the problem. @sclg, are you calling `ListScores()` twice? Because you should only call it once. – Jason P Apr 02 '14 at 14:04
  • Do I need parameters in ListScores in the .when line? I assumed that line was just saying 'wait for the ListScores function to return after it is called somewhere else'?? – sclg Apr 02 '14 at 14:07
  • @sclg Not as you have it written. You could wrap `ListScores` with another function and create a module that caches the response if you want to call it more than once. – Jason P Apr 02 '14 at 14:11
  • Nope, ListScores() invokes the function immediately and passes its return value, a Promise object in this case, to $.when(). – dgvid Apr 02 '14 at 14:13
  • Thanks both - You are both right of course - I had misunderstood the syntax. Now doing what I want. – sclg Apr 02 '14 at 14:13

1 Answers1

-1

Edit : after@ Jason P comment, I think this wrong solution But I will keep it as it shows how to autoinvoke a function ---End of edit

It is not firing because ListScores() was never invoked

You should wrap the ListScores() and immediately invoked it.

function ListScores(){

}

To invoke it

You should add the following

 (function ListScores(){

 })();

Got it?

stackunderflow
  • 3,811
  • 5
  • 31
  • 43