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 6566983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:16:56+00:00 2026-05-25T14:16:56+00:00

I am sure this is a simple question but have not been able to

  • 0

I am sure this is a simple question but have not been able to figure this out. The below shows a function that is created and added to a google map marker on the mousedown event. This is created for each marker iterated through.

The part eluding me is on line 5 below the pickupVenue(data[i].id) the value submitted in this function call is always the last iterated value. I want to stick a static value there of what I am iterating through when creating each marker.

for (i in data) {
    google.maps.event.addListener(marker, 'mousedown', function() {
        state = PINCH;
        map.setOptions({draggable: false});
        timeoutId = setTimeout(function() { pickupVenue(data[i].id) }, 1000); 
    });
}

—- working result —-

google.maps.event.addListener(marker, 'mousedown', pickupMarker(data[i].Listing.id));

function pickupMarker(id) {
    return function() {
        state = PINCH;
        map.setOptions({draggable: false});
        timeoutId = setTimeout('pickupVenue('+id+')', 1000);
    }
}
  • 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-25T14:16:57+00:00Added an answer on May 25, 2026 at 2:16 pm

    You need to make sure the function you pass as the event listener has a variable in its scope that references the value that i held in the loop without being modified i being modified.

    Currently all the functions you’re creating in the loop are sharing the same i variable, so they’ll all end up getting whatever value i references when the handler is finally invoked.

    In JavaScript, the only way to create a new variable scope is to invoke a function, and create a local variable (or parameter) in that function that references the value you want.

    Like this:

    function createListener( i ) {
        return function() {
            state = PINCH;
            map.setOptions({draggable: false});
            timeoutId = setTimeout(function() { pickupVenue(data[i].id) }, 1000); 
        };
    }
    
    for (i in data) {
        google.maps.event.addListener(marker, 'mousedown', createListener( i ) );
    }
    

    Here I created a createListener() function, and passed i into it.

    Because your listener function is now being created inside the createListener() function, that listener will reference the i in that variable scope, which will continue to retain whatever value it was given when you invoked createListener() (unless your function modifies i).

    Then createListener() returns that listener function, which is passed as the third argument to addListener().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im sure this will be a simple one but have a project that started
I'm pretty sure this is a simple question in regards to formatting but here's
Not sure this is a programming question, but we use LaTeX for all our
I'm sure this is a relatively simple question, and there must be a good
I am sure this is a very simple problem, but I am new to
This sure seems like a simple error to fix. However, I somehow can't figure
I am sure there is a simple answer to this one. I have a
This really has my stumped today. I'm sure its simple, but... Here is my
Pretty sure this question counts as blasphemy to most web 2.0 proponents, but I
I'm sure this question has been asked a million times, however I haven't been

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.