everybody! I'm working on some kind of an event calendar and I am using jQuery for the front-end and PHP for the back-end. I have all calendar data created with PHP and sent to the client, JSON-encoded.
Consider the following code (simplified):
jQuery(document).ready(function($) {
// property which contains calendar object
this.Calendar;
// get calendar object
show_month();
/* Assign a click event to the next month button */
$('#cal_month .next span').live('click', function() {
var month = $(this).attr('id');
var year = $(this).attr('class');
show_month(month, year);
});
function show_month(month, year) {
// delete pointers to the current month object
window.Calendar = undefined;
// get new month object
window.Calendar = get_month(month, year);
/* doing some stuff with this object here, like output to the
document etc., no event bindings */
}
function get_month(month, year) {
var calendar;
$.ajax({
type: 'GET',
dataType: 'json',
async: false,
timeout: 100,
url: '../some_url.php?month='+month+'&year='+year,
success: function(result) { calendar = result; }
});
return calendar;
}
var counter = 0;
while (counter != 1000) {
$('#cal_month .next span').trigger('click');
counter ++;
}
});
Switching months manually as well as making a loop results in a huge memory leak. I've read a bunch of info about memory leaks, js closures etc. here and generally on the internet but I guess I still have some misunderstanding. I's be glad to get some ideas about what is wrong in my code.
UPDATE
I am measuring memory usage watching firefox.exe (other browsers have the same trouble) in Task Manager. Memorry doesn't free even when I navigate from page. And after some time, it causes a great performance drop as I can make it use 600+ Mb.
I also have written a loop to switch months (added code above).
UPDATE 2
The reason I used global was that in my event calendar I also have week and day views, so I want an active month object to be available for functions that take care of those two views, and only destroy it and get the new one when user switches the month. My issue was exactly the usage of globals. That one solved, now trying to figure out how to keep an object available until user explicitly switches the month...
BTW, I've created a while loop for benchmarking purpose only, it has nothing to do with my actual code.