Answer the question
In order to leave comments, you need to log in
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/>
$("#slideInner").append(item.title);<br/>
});<br/>
});<br/>
$.getJSON('json.php?place=1&page=1', function(data){<br/>
$.each(data, function (i, item) {<br/>
$("#slideInner").append(item.title);<br/>
var records = 10;<br/>
});<br/>
});<br/>
alert (records);<br/>
Answer the question
In order to leave comments, you need to log in
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
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. За пределами этой функции она не определена.
});
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);
- We count the number of records using for ... in
- Make records global and it will be visible everywhere.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question