tags:

views:

174

answers:

3

Hi. I'm new here and I'm very stuck. I need help. Hopefully someone can tell me there's a very simple solution to my problem Smile

I have a site where you can click links and open the relevant content in a "window" so to speak. It's all from the same domain. Anyways, my problem occurs when you click on lets say... 5 links very fast. What happens is that only 1 or 2 of the windows get content. What's happening is that when one of the first calls finishes, it seems to take precedence over more recent calls.

Basically, I want to know how to handle multiple .get calls and load each one into a seperate window without interfering with the others. Here's the function that handles it:

popupWindow.prototype.openURL = function(link){
       windowname=this.windowname;
   theWindow="."+windowname+"_window .window .content";
   $(theWindow).hide();
        $(theWindow).html('<p class="page-loading"><img src="graphics/loading.gif" alt=""/></p>').fadeIn("medium");

   $.get(link, function(data) {
      $(theWindow).hide();
      $(theWindow).html(data).fadeIn("medium");
   });
}

The above code works just fine when you click on links... slowly. Or one at a time. If anyone can help, please let me know.

+4  A: 

Try that :

var theWindow = "." + this.windowname + "_window .window .content";

var will change the scope of your variable to the function, and hopefully solve this concurrency problem.

And... use Ids when possible...

ybo
I could use IDs for this I suppose. These windows are created from scratch. Something like that... I thought classes would be appropriate. There are a total of 5 possible windows... but each one has it's own relevant content I suppose. Thanks for pointing out my mistake though :)
jim2point0
You're welcome :)
ybo
+3  A: 

Your theWindow variable is an implied global variable. Each time openURL is called, theWindow is overwritten.

You can fix this problem simply by changing it to var theWindow = ..

Magnar
+1  A: 

You guys are amazing. I'm relatively new to javascript so I get my local variable\global variable\object properties mixed up easily. I solved the problem by localizing every variable declared in that function, and sending the data (once retrieved) to a separate function of that object (I called it "handleDataRequest(data)").

Now each window loads content into itself and doesn't conflict with the others!

Thanks guys. This was the biggest hurdle in my site thus far.

jumps up and down with excitement

jim2point0