A
A
Alexander2011-12-13 05:11:10
JavaScript
Alexander, 2011-12-13 05:11:10

JS Variable outside a function?

Hello.
I'm new to JS, but there was a need to implement several functions in this language and I ran into a small problem:

In general, there is a script executed after $(document).ready(function() , it receives data from a php script in json format and inserts them to a specific div

$.getJSON('json.php?place=1&page=1', function(data){<br/>
 $.each(data, function (i, item) {<br/>
 $(&quot;#slideInner&quot;).append(item.title);<br/>
 });<br/>
 });<br/>


The data is taken from the database and their number may vary. And so me interests, how it is possible to count an amount of the received records. And how then to use this value outside the function.

Just if you do it like this:

$.getJSON('json.php?place=1&page=1', function(data){<br/>
 $.each(data, function (i, item) {<br/>
 $(&quot;#slideInner&quot;).append(item.title);<br/>
var records = 10;<br/>
 });<br/>
 });<br/>
alert (records);<br/>


The alert will not output anything, since the scope of the records variable is limited.

I apologize for the stupid question, and its not quite competent statement, but I am writing at 6 in the morning without sleeping for about 20 hours.
Thanks for answers.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
mx2000, 2011-12-13
@mx2000

1. the number of records can be given in the JSON itself as meta information about the data, for example:
{"count": 100, "data": { ... 100 записей ... } }
2. as an alternative, you can iterate over the JSON structure: EMNIP, $.getJSON() is executed asynchronously, so the alert in your version will be called before the data is received, so it makes sense to generate some "data received" event or specify a continuation function in the getJSON() callback.
var records = -1; // начальное значение, "данные еще не посчитаны".
$.getJSON('json.php?place=1&page=1', function(data) {
records = 0;
$.each(data, function (i, item) {
$("#slideInner").append(item.title);
records++;
});
alert(records); // будет актуальное кол-во записей.
});
alert (records); // будет -1

A
Anton Korzunov, 2011-12-13
@kashey

In JS, everything is not like normal languages.
Initially, variables have a global scope. As if they are created at the beginning of the program.
The key var translates them into local visibility - as if they were created at the beginning of your function (and not lines or brackets)
, it turns out that Now you just need to remove var from records. $.each(data, function (i, item) {
$("#slideInner").append(item.title);
var records = 10; <-- обьявлена как VAR. За пределами этой функции она не определена.
});

G
Grigory Peretyaka, 2011-12-13
@Peretyaka

Arrays have a length property. If data is an array, then just data.length. It does not work correctly if strings are removed from the array, and jQuery-vsky each does not work in the best way. Or, perhaps more accurately, rows cannot be deleted.
Or just put a counter in each.
To make it work: I do not recommend using global variables, but the global variable is declared without var: records = 10; - we declared a global variable and assigned the value 10 to it. Well, or: window.records = 10; - I do this in order to know for sure that I did not forget to put var, and here is exactly that rare case when a global variable is needed, but I have not seen someone else write like that.
var records = 0;
$.getJSON('json.php?place=1&page=1', function(data){
$.each(data, function (i, item) {
$("#slideInner").append(item.title);
records = 10;
});
});
alert (records);

V
Vitaly Zheltyakov, 2011-12-13
@VitaZheltyakov

- We count the number of records using for ... in
- Make records global and it will be visible everywhere.

A
Anatoly, 2011-12-13
@taliban

in general, look in what form your json is, if it starts with [ then it is an array and, as mentioned above, it will have a length property, otherwise it is an object, and as you wrote even above, you can transfer the length by hand.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question