So I'm creating a simple Web Page that takes data from a json file and appends a number of paragraphs equal to the number of objects in the json file array:
$(function () {
function init() {
console.log("OK");
var dat;
var i;
load();
fill();
}
function load()
{
$.get("data.json", function (data, status) {
dat=data;
console.log(dat);
})
}
function fill()
{
console.log(dat);
for(i=0; i<dat.length(); i++) $("#container").append("<p>Testing</p> <br/><br/>")
}
$(".front").on("load", init());
});
However when I run the web page the first time I try to do a console log it prints out the data however the second time, inside the fill() function it says:
"Uncaught ReferenceError: dat is not defined"
Any ideas?