tags:

views:

121

answers:

3

Hi

I am getting a response from ajax request(done by jquery).

I have a method that displays errors to the users. Sometimes though I have some other information coming along with the json request.

So I don't want this info shown(where the rest of the errors are). So I figured since I always know the length of the json coming back I can just shorten the length since the error method just uses a while loop so if it is one less then it won't display that json part.

So I did this

var length = result.length -1;

ErrorsMethod(result, length); // call function.

   function ErrorsMethod(result, length)
    {
       while( i < length)
       { 
         // do stuff.
       }
    }

length is always undefined when it gets passed in though. I don't understand why.

I then tried

length.length ParseInt(length);

None seems to work. I don't even know what I am working with. When I do an alert of "length" var before it goes into the function it spits out a number.

A: 

Were you correctly calling parseInt?

var length = parseInt(result.length) - 1;
ErrorsMethod(result, length);

ErrorsMethod(result, length); // call function.

   function ErrorsMethod(result, length)
    {
       while( i < length)
       { 
         // do stuff.
       }
    }
karim79
Always supply the second parameter to parseInt, 10 in most cases. See http://stackoverflow.com/questions/850341
Warren Young
A: 
ErrorsMethod(result); // call function.

function ErrorsMethod(result)
{
    if (result && result.length)
    for ( var i = 0 ; i < result.length ; ++i )
    { 
        // do stuff.
    }
}
w35l3y
A: 

nor result or i are defined variables so thats probably why the script stops executing. try something like this:

<script type="text/javascript">
    var result = [10, 2];
    var length = result.length;


    ErrorsMethod(result, length); // call function.

    function ErrorsMethod(result, length)
    {
       var i=0;
       while( i < length)
       { 
         // do stuff.
         alert(i);
         i++;
       }
    }

</script>
kripto_ash