Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6580865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:04:51+00:00 2026-05-25T16:04:51+00:00

I have a javascript problem that I believe is related to closures in loops

  • 0

I have a javascript problem that I believe is related to closures in loops similar to:

How to copy a variable in JavaScript?
JavaScript closure inside loops – simple practical example

I’m plotting a list of pins on a Bing map and attaching an event to each pin so that an infobox can be displayed for each pin. Each pin is supposed to have a different info box. The problem is that when the event fires it always displays the very last info box from the last iteration of the loop, not the info box that goes with the pin firing the event. Here is the method with the looping construct and my failed attempt to create a closure

monumentMap.plotData = function() {
        monumentMap.theMap.entities.clear();

        var monument = null
        var loc = null;
        for (var i = monumentMap.theData.length - 1; i >= 0; i--) {
            monument = monumentMap.theData[i];
            loc = new Microsoft.Maps.Location(monument.lat, monument.lon);

            // construct a bing pushpin and add it to the item data
            monument.pin = new Microsoft.Maps.Pushpin(loc, {
                icon: 'http://media.maps101.com/' + monument.pinImg
            });

            // construct a bing infobox and add it to the item data
            monument.infobox = new Microsoft.Maps.Infobox(loc, { // breakpoint 1
                title: monument.title,
                offset: new Microsoft.Maps.Point(-3, monument.pin.getHeight() - 5),
                zIndex: 999,
                visible: true
            });

            // add event listeners
            // doesn't work
            Microsoft.Maps.Events.addHandler(monument.pin, 'mouseover', function() {
                return function(infobox) {
                    monumentMap.displayInfobox(infobox);
                }(monument.infobox); // breakpoint 2: by the time this is executed monument.infobox is already "last not current"
            });

            // add the entities to the map view for this item
            monumentMap.theMap.entities.push(monument.pin);
        }
}

I’m having trouble getting my head around why it doesn’t work. I set monument.infobox right there in the loop and if I put a breakpoint on the line marked “breakpoint 1” I can see that monument.infobox changes on every loop iteration, yet when I get to “breakpoint 2” (which doesn’t get executed until the event fires) it references the last infobox i created, not the infobox that I attached to that pin when attaching the event handler.

By this point I half-expect the line monumentMap.theMap.entities.push(monument.pin); to add a bunch of copies of the last pin in the loop, but it doesn’t. It works just as it should and adds a different pin each loop iteration.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:04:51+00:00Added an answer on May 25, 2026 at 4:04 pm

    You need to put the parameter in the outer anominous function. If I’m correct:

    Microsoft.Maps.Events.addHandler(monument.pin, 'mouseover', 
    (function(infobox) {
      return function() {
        monumentMap.displayInfobox(infobox);
      }     
     })(monument.infobox));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem in some JavaScript that I am writing where the Switch statement
I have a problem with a javascript set of functions that I made. This
My problem is so simple, that I cannot believe that there is no HTML/CSS
So I have a rather basic javascript problem which I have been slamming my
I have problem with dynamically created image (JavaScript). I want to change the innerHTML
I have a problem with a javascript error: $(#slider) is undefined How can i
Hi I have a problem with a javascript string var foo = \<a href=javascript(foo('a','b'))>test</a>\
I have the following problem: <script type=text/javascript> alert(1. ČĆŽŠĐčćžšđ); </script> <script type=text/javascript src=Tst.js></script> <script
I am having a problem accessing the ViewData object through javascript. I have set
I have this html code <html> <head> <title>JQuery Problem 2</title> <script type=text/javascript src=jquery-1.4.min.js></script> <script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.