beginner trying to wrap my head around javascript and a scope issue im having.
Im pulling some json data and pushing it into an array called zero[] .. Getting it to work outside the anonymous function has failed.
What can i do to get zero[] to work outside the function?
<script>
var addr = "data657";
var zero = [];
var full = [];
$(function(){
jQuery.getJSON('https://jsondataurl.com/'+addr, function(result) {
if (result == 0)
zero.push(addr);
if (result > 0)
full.push(addr);
var j = Math.floor(Math.random()*4);
alert(zero[j]); /// works great here !!!
});
});
alert(zero[j]); // DOES NOT work here
$("#foo").attr("data-stuff", zero[j]); // DOES NOT work here
</script>