Global variable not valid in callback function?

I am using jQuery 1.4.2 and I am trying to carry over the global variable totaltabs into my JSON callback function. The code seems to work properly, however, in my loadJSON function after the callback function loads the data, my totaltabs variable will not increment.

Here is a peak at my code. Any ideas?

totalItems is loaded from another function, not relevant to my example.

var totaltabs = 0;


	for(i = 0; i < totalItems.length; i++){


function loadJSON(params) {
		if (data.TotalItems == 0) {
			$("#tab-"+totaltabs+" ul").append('<li><div class="wrap"><p>No items found.</p></div></li>');
		else {
			$.each(data.Item, function(i,item){
				var title = item.Title;
				if (null != title)
					$("#tab-"+totaltabs+" ul").append('<li><div class="wrap"><div class="desc">' + title + '</div></div></li>');
		$("#tab-"+totaltabs+" .title a").attr("href", data.ItemURL);
		$("#tab-"+totaltabs+" .title a").append(" (" + data.TotalItems + ")");


Try using a globally accessible global variable.

var window.MYPROJECTNAME.totaltabs = 0;
    // it's okay to assign totaltabs locally now, as it's not being updated from here on in.
    var totaltabs = window.MYPROJECTNAME.totaltabs;