Javascript Variable Scope Question

I have the following javascript for a YouTube API application.  It takes an array of YouTube video ID's (bottomVids) and a Title string (theTItle) and loops through each video, using the YouTube API to get the thumbnails.

At the begining of the function, I set "bottomDesc" as an empty variable.  This variable will be used to  build the HTML that I will later put into the page using jQuery.

For some reason, when I get out of the $.getJSON block, "bottomDesc" is empty - as if it hadn't been set.
If I do an alert inside the $.getJSON block, the value is set.

I thought the variable would stay in scope for the whole function, but for some reason it's not.  Any ideas on what i can do to pass values to and use a variable this way?  

function changevids(bottomVids, theTitle) {	

		var videoDesc = "";
		var subDesc = "";
		var stats = "";
		var bottomDesc = "";
		var derClassin = "triovid";
		var videoImg, trioImg, viewCount;
		var bottomNums = bottomVids.length;
		alert("array length: " + bottomNums);
		var newtitle = theTitle;
		var g;
		var url = "";
//Middle Videos (3)
		for (i=0;i<bottomNums;i++){

			url = '' +bottomVids[i] + '&part=snippet,contentDetails,statistics,status'

			$.getJSON(url,function(data) {
				vidResults = data.pageInfo.resultsPerPage;
				for(var i=0;i<vidResults;i++) {
			   		vidID = data.items[i].id.videoId;
					title = data.items[i].snippet.title;
					hqImage = data.items[i].snippet.thumbnails.high;
					stats = data.items[i].statisitcs.viewCount;
					g = i + 1
					if (g % 3 == 0) {
						derClassin = derClassin + ' lastvid'; 		
						derClassin = 'triovid';		
					bottomDesc += '<div class="col-md-4 '+derClassin+'"><a onclick="swapvids(\''+ vidID +'\');" href="#"><img src="'+ title +'" width=200px height=122px border=0 /></a><p style="text-align:left;"><span class="vidTitle">'+ title +'</span><br/><span class="numViews">'+ stats +' views</span></p></div>';
			// an alert to check the variable - it's empty for some reason...
                       alert("144 bd: " + bottomDesc);
		bottomDesc += '<div style="clear:both;"></div>';
                //this alert shows just the '<div style="clear:both;"></div>' - but none of the information that should've been set inside the loop.
		alert("BD" + bottomDesc);
		$('#main_video').html('<iframe width="636" height="358" src="//'+bottomVids[0]+'?wmode=transparent" frameborder="0" allowfullscreen wmode="Opaque"></iframe>');
		$('#vidDescription').html('<h2 style="font-family:arial; font-size:20px; color:#bc8e30; margin-top:15px; margin-bottom:0px; border-bottom:#bc8e30 solid 1px; padding-bottom:5px;">' + newtitle + '</h2>');

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Here's info on 'scope' in javascript:  It says that if you declare the variable inside a function like you are doing, it exists only within the scope of that function.  If you declare it before and outside of the function, then it will be global.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightlyoffAuthor Commented:
Thanks for the quick response.  I declared the variable in the function and used the variable in the function - but it still seems to lose scope.

I will check out the article though and see if im missing something.  Thanks!
slightlyoffAuthor Commented:
I had to change my getJson's to ajax and turn async off.  That made it work.
Thanks for the help.
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad you got it working.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.