Andrew February 2016

What causes the $.active variable to decrement?

I am debugging an issue where the $.active value is not being decremented after making an AJAX request to load an iFrame dynamically (this is after the page has already loaded). Here is the source code for loading the iFrame:

        function initializeiFrame(id, frameborder, iFrameSrc) {
           var iFrameSelector = $('<iframe id="' + id + 'iFrame" name="' + id + '" allowtransparency="true" scrolling="no" frameborder="' + frameborder + '" style="display: none; top: 0px; left: 0px; position: absolute">&nbsp;</iframe>');
           iFrameSelector.appendTo("#hiddenIFrameContainer");
           iFrameSelector.attr('src', iFrameSrc);
           return iFrameSelector;
        }

        iFrameSelector = initializeiFrame(id, frameborder, iFrameSrc);

        iFrameSelector.load(function() {
           doWorkAfteriFrameLoad(...);
        });

This code works fine the first time I load an iFrame. The second time this code is invoked (for a different iFrame but reusing the same code), it locks up because in our application we check the $.active value before allowing an AJAX request to be sent (we limit the number of requests). Apparently, when loading an iFrame dynamically, the $.active value is not being decremented. So the next time one is loaded, the $.active value is too high and the message is never sent to the server. Any ideas?

Answers


Andrew February 2016

In case anyone is running into a similar problem, I figured it out (after 2 days of banging my head on it). I was performing this work inside an event handler that wasn't calling event.preventDefault() beforehand. Apparently, not including this code had a very nasty side effect of double incrementing the $.active variable. I still don't know how the hell it could have done that because even when I was debugging jQuery itself, I could never see it changing to 2. If I added console.log or debugger statements, the problem would go away. So it seems like to me it's some kind of very strange race condition. Anyway, that's how I fixed it so that someone else maybe doesn't have to go through the horrible pain I did.

Post Status

Asked in February 2016
Viewed 3,085 times
Voted 10
Answered 1 times

Search




Leave an answer