0

How can i count or check the length of the return function

function leavereminder() {
        $.getJSON("<?=base_url()?>home/leavereminder",
            {},
            function(data) {
                if(data.length != 0) {
                    for(x=0; x<data.length; x++) {
                        var lblm = document.createElement('div');        
                        lblm.innerHTML = '<label>'+data[x]+'</label>';  
                        lblm.className = 'alert alert-info';                  
                        document.getElementById('notifbody').appendChild(lblm);           
                    }
                }
                var b = data.length;
        });
    }

I want my variable b to be global variable so i can fetch the length

elixenide
  • 44,308
  • 16
  • 74
  • 100
kry
  • 3
  • 4
  • Probably a duplicate or at least related: [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – t.niese Jun 30 '14 at 04:43

4 Answers4

2

You cannot return back values from async calls. Instead you can use callbacks to know when async call is completed. In your example :

leavereminder(function(length) {
    console.log(length); // This will get you your data length immediately after JSON call is completed.
});
function leavereminder(callBack) {
    $.getJSON("<?=base_url()?>home/leavereminder",
        {},
        function(data) {
            if(data.length != 0) {
                for(x=0; x<data.length; x++) {
                    var lblm = document.createElement('div');        
                    lblm.innerHTML = '<label>'+data[x]+'</label>';  
                    lblm.className = 'alert alert-info';                  
                    document.getElementById('notifbody').appendChild(lblm);           
                }
            }
            // Instead of this : var b = data.length;
            callBack(data.length); // This will send the length back(alternate for return)
    });
}

Problem with using Global variables is you will never know when the call completes(unless you add an observer but why not keep it simple?)

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
1

It's not clear what you mean by "check the length of the return function," but I think you mean check the value of b. In that case, just declare b outside your callback, like this:

var b = 0;
function leavereminder() {
    $.getJSON("<?=base_url()?>home/leavereminder",
        {},
        function(data) {
            if(data.length != 0) {
                for(x=0; x<data.length; x++) {
                    var lblm = document.createElement('div');        
                    lblm.innerHTML = '<label>'+data[x]+'</label>';  
                    lblm.className = 'alert alert-info';                  
                    document.getElementById('notifbody').appendChild(lblm);           
                }
            }
            b = data.length;
    });
}

P.S. You should use a more descriptive variable name than b - this name is hard to debug and may well conflict with other scripts on the page.

elixenide
  • 44,308
  • 16
  • 74
  • 100
1

If you want b to be global then you need to initialize it outside the function. Then b will accessible by leavereminder().

var b;

function leavereminder() {
        $.getJSON("<?=base_url()?>home/leavereminder",
            {},
            function(data) {
                if(data.length != 0) {
                    for(x=0; x<data.length; x++) {
                        var lblm = document.createElement('div');        
                        lblm.innerHTML = '<label>'+data[x]+'</label>';  
                        lblm.className = 'alert alert-info';                  
                        document.getElementById('notifbody').appendChild(lblm);           
                    }
                }
                b = data.length;
        });
    }
  • I initialize the value of b like this var b = 0; leavereminder(); alert(b); but when i alert after the function it is still 0 – kry Jun 30 '14 at 04:33
  • @kry so what are you trying to do, exactly? –  Jun 30 '14 at 04:34
  • @kry but you also define you `var b` inside of the callback function you pass to `getJSON` so the `b` inside of that and the one outside of it are two different `b`. Anyway it looks like you run into the the problem that those callbacks are async. – t.niese Jun 30 '14 at 04:35
  • @t.niese yess thats my problem.. is there a way i can pass the value from my json variable b to global variable b?? – kry Jun 30 '14 at 04:37
  • @kry thats what _josh_ and _Ed Cottrell_ made, by removing the `var` definition inside the callback. If this does not work for you, then you should describe your actual problem in your question. – t.niese Jun 30 '14 at 04:40
0

Write it in hidden html input field with jquery and use it later.

In your ajax call:

$('#count_var').val(data.length);

Your html to collect the count:

<input type="hidden" name="count_var" id="count_var" />
DarioBB
  • 663
  • 2
  • 8
  • 29