I am working on a thing that eats JSON and constructs a sequential gallery of images and videos. I am using jQuery. Here's the basic idea:
So the narrative is, when I successfully get the JSON, I set up the gallery and allow people to click back and forth through the images/videos. Then when they're done, they click #close, which calls hideGallery and re-displays the main page content. My understanding is that .remove() in jQuery removes elements from the DOM as well as all EventListeners and other structures associated with them. So 'slides' should clear once the first $.getJSON goes out of scope. Then when the user clicks another gallery, $.getJSON should run again, 'slides' should be re-defined, and all should be well.
This was all working great on Friday, however today every time I call $.getJSON, my "slides" array continues to have the data from the first call in it. This is doubly weird because it's pulling the correct JSON from the server, and the first image of the "new" gallery loads fine. However, trying to click through the images doesn't work at all.
My understanding of Javascript scope is that every time $.getJSON succeeds, I should be allocating a new "slides" variable which contains the result of my JSON call. On Friday this is how it was working, however today it seems to have stopped. Is there something obvious here I am missing? Am I inadvertently creating a closure here? I haven't made significant changes to other sections of the code, so I am pretty stumped as to what changed over the weekend.
Thanks for taking a look!
Code:
$.getJSON('http://my.awe/some/url/', '', function(data){
var slides = data.slides;
var curSlide = 0;
/*** SNIPPED - some other gallery setup ***/
function updateSlide(){
slideStage.html('<img src='/html//civis/threads/+slides[curSlide].image+'>');
}
$('.nextSlide').live('click', function(){
curSlide++;
updateSlide();
return false;
});
$('.prevSlide').live('click', function(){
curSlide--;
updateSlide();
return false;
});
$('#close').live('click', hideGallery);
}
function hideGallery(){
$('#slideshow').hide(800, function(){
$(this).remove();
$('#main-content').show();
});
return false;
}
So the narrative is, when I successfully get the JSON, I set up the gallery and allow people to click back and forth through the images/videos. Then when they're done, they click #close, which calls hideGallery and re-displays the main page content. My understanding is that .remove() in jQuery removes elements from the DOM as well as all EventListeners and other structures associated with them. So 'slides' should clear once the first $.getJSON goes out of scope. Then when the user clicks another gallery, $.getJSON should run again, 'slides' should be re-defined, and all should be well.
This was all working great on Friday, however today every time I call $.getJSON, my "slides" array continues to have the data from the first call in it. This is doubly weird because it's pulling the correct JSON from the server, and the first image of the "new" gallery loads fine. However, trying to click through the images doesn't work at all.
My understanding of Javascript scope is that every time $.getJSON succeeds, I should be allocating a new "slides" variable which contains the result of my JSON call. On Friday this is how it was working, however today it seems to have stopped. Is there something obvious here I am missing? Am I inadvertently creating a closure here? I haven't made significant changes to other sections of the code, so I am pretty stumped as to what changed over the weekend.
Thanks for taking a look!